I have two Json arrays like below:
$a='[{"type":"text","req":0,"name":"user"},{"type":"text","req":0,"name":"org"},'
. '{"type":"textarea","label":"Notes","req":0},'
. '{"type":"text","label":"text1","req":0},'
. '{"type":"textarea","label":"Notes","req":0},'
. '{"type":"text","label":"text2","req":0},'
. '{"type":"textarea","label":"Notes","req":1}]';
$b='[{"type":"textarea","label":"Notes","Element_Values":"331","Element_Name":"textarea-710091","Count_Images":0},'
. '{"type":"text","label":"text1","Element_Values":"1","Element_Name":"text-987351","Count_Images":0},'
. '{"type":"textarea","label":"Notes","Element_Values":"332","Element_Name":"textarea-254458","Count_Images":0},'
. '{"type":"text","label":"text2","Element_Values":"2","Element_Name":"text-3410","Count_Images":0},'
. '{"type":"textarea","label":"Notes","Element_Values":"333","Element_Name":"textarea-554051","Count_Images":0}]';
As you can see array 'a' starts with few keys which is not in array 'b'. I want to skip the arrays which has 'name' keys.
I did the following code, but didnt work:
$c = [];
$aJson=json_decode($a, true);
$bJson=json_decode($b, true);
foreach($aJson as $key => $array)
{
foreach($array as $an)
{
if(array_key_exists('name', $an))
{
//continue;
}
}
$c[$key] = array_merge($bJson[$key],$array);
}
echo json_encode($c);
The result array c should be:
[{"type":"textarea","label":"Notes","Element_Values":"331","Element_Name":"textarea-710091","Count_Images":0,"req":0},{"type":"text","label":"text1","Element_Values":"1","Element_Name":"text-987351","Count_Images":0,"req":0},{"type":"textarea","label":"Notes","Element_Values":"332","Element_Name":"textarea-254458","Count_Images":0,"req":0},{"type":"text","label":"text2","Element_Values":"2","Element_Name":"text-3410","Count_Images":0,"req":0},{"type":"textarea","label":"Notes","Element_Values":"333","Element_Name":"textarea-554051","Count_Images":0,"req":1}]
Please help me
This is a simple debug problem, $array is already the array.
$index = 0;
foreach($aJson as $key => $array)
{
if(isset($array['name']))
continue;
$c[$index] = array_merge($bJson[$index], $array);
$index++;
}
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
First of all I have searched for the similar threads on StackOverflow like this one
$myvar = array ("key_name" => array("tom", "an", "bob"),
"key_age" => array("1", "10", "12")
);
I have tried lot of things but I couldn't
foreach($myvar as $i){
foreach ($i as $key => $value) {
echo print_r($i);
}
I am trying to get "key_name" and loop through it
<?php
$myvar = array (
"key_name" => array("tom", "an", "bob"),
"key_age" => array("1", "10", "12")
);
foreach ($myvar['key_name'] as $value) {
echo $value;
}
Result:
tomanbob
https://3v4l.org/tEFvS
If you want to loop through both:
foreach ($myvar as $sub_array) {
foreach ($sub_array as $value) {
echo $value;
}
}
Result:
tomanbob11012
https://3v4l.org/cMFUh
Check out, http://php.net/manual/en/control-structures.foreach.php for info on using foreach
You can use array_walker. So you can do something like this :
array_walk($myvar,function($sub_items,$key){
echo "Key is >> " . $key . "\n";
foreach($sub_items as $item){
echo $item . "\n";
}
echo "------------ \n ";
});
Note:
I put an echo with a new line to understand how to implement that!
this code works as expected
$fruits = array("d" => "Zitrone", "b" => "Banane", "c" => "Apfel");
$fruits["a"] = "Orange";
//print_r($fruits);
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
ok, now I would like to do the same programmatically:
$collection = array();
foreach ($xml->abschnitte[0]->abschnitt as $sec) {
//echo $sec['strecke']; // this works, the strings are printed out
$collection[$sec['strecke']] = $sec['id'];
}
//print_r($collection); // nothing to see here
//ksort($collection);
foreach ($collection as $key => $val) {
echo $key . " = " . $val . "\n";
}
it seems that there no collection will be build. but there must be a way to build the key up from variables. what do i miss? thanks in advance, mischl
I have an associative array. Two dimensions which I am iterating through like this
foreach ( $order_information as $sector_index => $sector_value ){
echo 'sector : ' . current($order_information) ;
echo '<br>';
foreach ( $sector_value as $line_index => $line_value ){
}
}
The current() is an attempt to get the iteration the loop is in. It seems like this should give me that. However, elsewhere on that page there is the suggestions that you just do like
$index = 0
foreach ( $array as $key => $val ) {
echo $index;
$index++;
}
I wonder if I am using current incorrectly, as echo 'sector : ' . current($order_information); just prints sector : Array
Is $index++ bad syntax? Is there a better way to do this?
Answer
As far as I know there is no build in numeric counter in a foreach loop in PHP.
So you need your own counter variable. Your example code looks quite good to do that.
$index = 0;
foreach($array as $key => $val) {
$index++;
}
By the way, $index++; is fine.
Example
Here is an example illustrating which variable stores which value.
$array = array(
"first" => 100,
"secnd" => 200,
"third" => 300,
);
$index = 0;
foreach($array as $key => $val) {
echo "index: " . $index . ", ";
echo "key: " . $key . ", ";
echo "value: " . $val . "\n";
$index++;
}
The output will be that.
index: 0, key: first, value: 100
index: 1, key: secnd, value: 200
index: 2, key: third, value: 300
Current-Function
I think you misunderstood current($array). It gives you the value pointed by an internal array pointer, which can be moved using next($array), prev($array) and end($array).
Take a look at the manual to make thinks clear.