-1

I have an associative array

array(
    'item_name1' => 'PCC',
    'item_name2' => 'ext',
    'item_number1' => '060716113223-13555',
    'item_number2' => '49101220160607-25222)',
)

What i Want to do is catch all the array keys where the key name has similarities

for example i want to echo out item_name (it should get both item_name1 & item_name2) but i require it in a loop (foreach/for) so that i can send within the loop the details to my database for each set of values

Thanks For the help

2
  • my array has item_name1 and item_name2 i want to echo it out in a loop withouth using arrayname['item_name1']; Commented Jul 6, 2016 at 7:29
  • 1
    you have to provide more details like what you actually want to achieve through your expected outcome and what you have tried so for? Commented Jul 6, 2016 at 7:32

3 Answers 3

1

Use the funtion array_keys

$allKeys = array_keys($yourArray);
$amountKeys = count($allKeys);

Unless you do not provide more code this will give you all the keys of $yourArray Reference - array_keys

To get all the similar keys you can use this function similar-text()

Since I do not know how "similar" a key can be I would suggest you to test out different values and find a degree that matches your expectations.

Sign up to request clarification or add additional context in comments.

Comments

0

Your data model seems to use numeric suffixes as a replacement for arrays so I'll assume that key name has similarities is an overestimated problem statement and you merely want to fix that.

The obvious tool is regular expressions:

$original_data = array(
    'item_name1' => 'PCC',
    'item_name2' => 'ext',
    'item_number1' => '060716113223-13555',
    'item_number2' => '49101220160607-25222)',
);

$redacted_data = array();
foreach ($original_data as $key => $row) {
    if (preg_match('/^(.+)(\d+)$/u', $key, $matches)) {
        $redacted_data[$matches[1]][$matches[2]] = $row;
    } else {
        $redacted_data[$key][] = $row;
    }
}
var_dump($redacted_data);

It should be easy to tweak for your exact needs.

I can't figure out what the i require it in a loop (foreach/for) requirement means but you can loop the resulting array as any other array:

foreach ($redacted_data as $k => $v) {
    foreach ($v as $kk => $vv) {
        printf("(%s,%s) = %s\n", $k, $kk, $vv);
    }
}

5 Comments

the array got combined like this which seem perfect array( [item_name] => Array ( [1] => Potrait Color Correction [2] => Extraction )
That's different data that you shared. Whatever, make sure you actually understand the code and don't forget to add your own safety checks (for instance, you may not want to overwrite existing keys).
the array got combined like this which seem perfect codearray( [item_name] => Array ( [1] => PCC [2] => Ext )[item_number] => Array ( [1] => 060716113223-13555 [2] => 49101220160607-25222 )) whcih seems now organized Now In Order To Print both of them foreach ($redacted_data['item_name'] as $abc => $val) { foreach ($redacted_data['item_number'] as $xyz => $valu) { echo $val.'<br>';echo $valu; } }code
thats exacty what i was gonna aks to prevent the values from replicating Potrait Color Correction 060716113223-13555Potrait Color Correction 49101220160607-25222Extraction 060716113223-13555Extraction 49101220160607-25222 like this
I used this to echo out the values without repetition but if there is a better way to do it could you suggest ` $arr=array(); foreach ($redacted_data['item_name'] as $abc => $val) { foreach ($redacted_data['item_number'] as $xyz => $valu) { if(!in_array($val, $arr)){ echo $val." ";echo $valu; } $arr[]=$val; $arr[]=$valu; } } `
0
<?php

$items=[];
$itemsNumbers=[];
foreach($itemarr as $key=> $val)
{
    $itempos = strpos("item_name", $key);
    if ($pos !== false) 
        $items[]=$val;
    $numberpos = strpos("item_number", $key);
    if ($numberpos !== false) 
        $itemsNumbers[]=$val;

}

?>
Note: here $itemarr is your input array

$items you will get list of item names and $itemsNumbers you will get list of itemsNumbers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.