I have 3 arrays
$description[$cat_id][$kit_id][$item_id] = $description ;
$description2[$cat_id][$kit_id][$item_id] = $description2 ;
$qty_total[$cat_id][$kit_id][$item_id] = $qty;
foreach ($qty_total AS $key => $val) {
echo "Cat: ".$key."<br />";
foreach ($val AS $kkey => $kval) {
echo " Kit: ".$kkey."<br />";
foreach ($kval AS $ikey => $ival) {
echo " Item: ".$ikey." - ".$ival."<br />";
}
}
}
This brings only qty.
My question is how to get qty, description, description2 for given [cat_id][kit_id][item_id]?
You need to use keys of all foreach() loop to access other variables value
foreach ($qty_total AS $key => $val) {
foreach ($val AS $kkey => $kval) {
foreach ($kval AS $ikey => $ival) {
echo $ival;
echo PHP_EOL;
echo $description2[$key][$kkey][$ikey];
echo PHP_EOL;
echo $description[$key][$kkey][$ikey];
}
}
}
Sample output: https://3v4l.org/9uYfT
Related
I'm trying out the following code for arrays in php, I create a associate array, print out the values and add one more to the array - print out again. This works, but if I try the foreach ($MovieCollection as $key => $value) it does not print out the values. Why does it not do that?
$myArray = array("Star Wars", "The Shining");
foreach ($myArray as $val)
{
echo("Movie: " . $val ."<br>");
}
$MovieCollection = array();
$MovieCollection[] = array('title' => 'Star Wars', 'description' =>'classic');
foreach ($MovieCollection as $film )
{
echo($film['title'] .": " . $film['description'] ."<br>");
}
$MovieCollection[] = array('title' => 'The shinning', 'description' =>'creepy');
foreach ($MovieCollection as $film )
{
echo($film['title'] .": " . $film['description'] ."<br>");
}
echo("<br><br>");
// This does not print the values?
foreach ($MovieCollection as $key => $value)
{
echo($key .": " . $value ."<br>");
}
That is because in this part $MovieCollection is an array of arrays and if you want to echo the $value which is an array, you will do an Array to string conversion which does not work.
What you might do is use another foreach to show the values per array:
foreach ($MovieCollection as $value) {
foreach ($value as $k => $v) {
echo($k .": " . $v ."<br>");
}
}
See a Php demo
$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
print_r($suppliername);
print_r($vals);
foreach($Product as $index => $value)
{
if($vals[$index]>1)
{
echo $suppliername[$index]."+++"."Multiple Entries";
// Here i have to get S0001
}
else
{
echo $suppliername[$index]."+++"."Single Entry";
// Here i have to get S0002
}
}
how to pass $index to array value? I am trying to check value of the index is greater than or not in if condition. How to get this?
With your code now it couldn't work since your print_r($val) result was :
Array
(
[S0001] => 2
[S0002] => 1
)
The key of your $vals are the result of your $suppliername array, so try like this maybe :
$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
print_r($suppliername);
print_r($vals);
foreach($Product as $index => $value)
{
if($vals[$suppliername[$index]]>1)
{
echo $suppliername[$index]."+++"."Multiple Entries";
}
else
{
echo $suppliername[$index]."+++"."Single Entry";
}
}
The result :
S0001+++Multiple Entries
S0002+++Single Entry
S0001+++Multiple Entries
Is this what you are looking for?
I'm not sure i understood well what you wanted, but here it does what you want as result :
<?php
$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
echo "<pre>";
var_dump($suppliername);
echo "</pre>";
echo "<pre>";
var_dump($vals);
echo "</pre>";
foreach($Product as $index => $value)
{
if($index>1)
{
echo "<br />" . $suppliername[$index]."+++"."Multiple Entries";
// Here i have to get S0001
}
else
{
echo "<br />" . $suppliername[$index]."+++"."Single Entry";
// Here i have to get S0002
}
}
Below is a simple array that I created:
$colors = array(
"parent1" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
),
"parent2" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
)
);
What I need to get is the key of my level 1 arrays which are string "parent1" and "parent2".
Currently I'm using foreach with while loop to get the key
foreach ($colors as $valuep) {
while (list($key, $value) = each($colors)) {
echo "$key<br />";
}
}
but I'm only able to get the "parent2" string from using the above method and not "parent1".
You're so close.
foreah($colors as $key => $val)
{
echo $key . "<br/>";
}
Use the key like so:
foreach ($colors as $key => $value) {
echo $key.'<br>';
}
To print out the keys:
foreach ($colors as $key => $value) {
echo $key . '<br />';
}
You can also get all of the keys from an array by using the array_keys() method, for example:
$keys = array_keys($colors);
foreach ($keys as $key) {
echo $key . '<br />';
}
I need to work out how i can get showBtn(3) to match up against the first result in every other key.
ShowBtn/3
btnMenulink/101
btnArticleLink/2
btnPhone/036244789
btnUrl/
btnName/Office
PHP:
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$array = json_decode($jsonresult,true);
foreach ($array as $key => $value) {
foreach ($value as $next_key => $next_value) {
echo $key . ":" . $next_key . ":" . $next_value . "\n";
}
}
I want this:
if (showBtn == 3) {
echo '<a href='tel:btnPhone'>btnName</a>';
}
the result would be
Office Mobile
I almost have it!
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$parsed = json_decode($jsonresult,true);
echo 'Showbtn: '.$parsed['showBtn'][0].' Phone: '.$parsed['btnPhone'][0].' Name: '.$parsed['btnName'][0];
echo '<hr/>Showbtn: '.$parsed['showBtn'][1].' Phone: '.$parsed['btnPhone'][1].' Name: '.$parsed['btnName'][1];
Now i just gotta get that [0] [1] into the loop somehow
I have it but can it be done better?
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$parsed = json_decode($jsonresult,true);
$i=0;
foreach ($parsed as $key => $value) {
if ($parsed['showBtn'][$i] == 3) {
echo 'Showbtn: '.$parsed['showBtn'][$i].' Phone: '.$parsed['btnPhone'][$i].' Name: '.$parsed['btnName'][$i].'<hr/>';
}
$i++;
}
This'll work for you. You need to work it as this way
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$array = json_decode($jsonresult,true);
foreach($array['showBtn'] as $key => $value){
if($value == 3){
echo ''.$array['btnName'][$key].'<br>';
}
}
Output:
Office
Mobile
This is an array i have
<?php
$page['Home']='index.html';
$page['Service']='services.html';
?>
How do i get to echo something like this for individual one like
Home is at index.html
and again how can i do this through a loop and echo all?
foreach($page as $key => $value) {
echo "$key is at $value";
}
For 'without loop' version I'll just ask "why?"
Without a loop, just for the kicks of it...
You can either convert the array to a non-associative one, by doing:
$page = array_values($page);
And then acessing each element by it's zero-based index:
echo $page[0]; // 'index.html'
echo $page[1]; // 'services.html'
Or you can use a slightly more complicated version:
$value = array_slice($page, 0, 1);
echo key($value); // Home
echo current($value); // index.html
$value = array_slice($page, 1, 1);
echo key($value); // Service
echo current($value); // services.html
If you must not use a loop (why?), you could use array_walk,
function printer($v, $k) {
echo "$k is at $v\n";
}
array_walk($page, "printer");
See http://www.ideone.com/aV5X6.
Echo key and value of an array without and with loop
$array = array(
'kk6NFKK'=>'name',
'nnbDDD'=>'claGg',
'nnbDDD'=>'kaoOPOP',
'nnbDDD'=>'JWIDE4',
'nnbDDD'=>'lopO'
);
print_r(each($array));
Output
Array
(
[1] => name
[value] => name
[0] => kk6NFKK
[key] => kk6NFKK
)
for the first question
$key = 'Home';
echo $key." is at ".$page[$key];
function displayArrayValue($array,$key) {
if (array_key_exists($key,$array)) echo "$key is at ".$array[$key];
}
displayArrayValue($page, "Service");
How to echo key and value of an array without and with loop
$keys = array_keys($page);
implode(',',$keys);
echo $keys[0].' is at '.$page['Home'];
My version without a loop would be like this:
echo implode(
"\n",
array_map(
function ($k, $v) {
return "$k is at $v";
},
array_keys($page),
array_values($page)
)
);
array_walk($v, function(&$value, $key) {
echo $key . '--'. $value;
});
Learn more about array_walk
A recursive function for a change;) I use it to output the media information for videos etc elements of which can use nested array / attributes.
function custom_print_array($arr = array()) {
$output = '';
foreach($arr as $key => $val) {
if(is_array($val)){
$output .= '<li><strong>' . ucwords(str_replace('_',' ', $key)) . ':</strong><ul class="children">' . custom_print_array($val) . '</ul>' . '</li>';
}
else {
$output .= '<li><strong>' . ucwords(str_replace('_',' ', $key)) . ':</strong> ' . $val . '</li>';
}
}
return $output;
}
You can try following code:
foreach ($arry as $key => $value)
{
echo $key;
foreach ($value as $val)
{
echo $val;
}
}