I'm trying to add onto an array while looping, although i'm not able to figure out how exactly to do this:
<?php
$original = array (
array ("title" => "one",
"color" => "blue"
),
array ("title" => "two",
"color" => "green"
)
);
$merged = array();
$str = "000three000red0!000four000white0!000five000black0!";
$pat = "/\d+(\D+)\d+(\D+)\d!/um";
preg_match($pat, $str, $match);
foreach($match as $result) {
$merged = array_merge($original,array("title" => $match[1], "color" => $match[2]));
print_r($merged);
}
The first problem is that is only seems to pick up the first match, the second being nothing ever gets added to $merged. I was hoping to have it output as:
Array
(
[0] => Array
(
[title] => one
[color] => blue
)
[1] => Array
(
[title] => two
[color] => green
)
[2] => Array
(
[title] => three
[color] => red
)
[3] => Array
(
[title] => four
[color] => white
)
[4] => Array
(
[title] => five
[color] => black
)
)
Full, including the preg_match_all:
$original = array (
array ("title" => "one",
"color" => "blue"
),
array ("title" => "two",
"color" => "green"
)
);
$merged = array();
$str = "000three000red0!000four000white0!000five000black0!";
$pat = "/\d+(\D+)\d+(\D+)\d!/um";
preg_match_all($pat, $str, $match);
$merged = $original;
$i = 0;
foreach($match[1] as $result) {
$merged[] = array("title" => $match[1][$i], "color" => $match[2][$i]);
$i++;
}
print_r($merged);
results in:
Array (
[0] => Array
(
[title] => one
[color] => blue
)
[1] => Array
(
[title] => two
[color] => green
)
[2] => Array
(
[title] => three
[color] => red
)
[3] => Array
(
[title] => four
[color] => white
)
[4] => Array
(
[title] => five
[color] => black
)
)
The problem is:
foreach($match as $result) {
$merged = array_merge($original,array("title" => $match[1], "color" => $match[2]));
print_r($merged);
}
In each step of your loop you merge original array with new array and save the output into merged array so in fact you don't change original array and each time you set merged value again.
Change it into:
$merged = array(); // or $merged = $original; depending on your exact needs
foreach($match as $result) {
$merged = array_merge($merged,array("title" => $match[1], "color" => $match[2]));
print_r($merged);
}
Related
// ARRAY
(
[apple] => one
[orange] => two
[strawberry] => three
)
// NEW ARRAY
(
[0] => Array
(
[0] => apple
[1] => one
)
[1] => Array
(
[0] => orange
[1] => two
)
[2] => Array
(
[0] => strawberry
[1] => three
)
)
I want to create an array for each key of the current array, so i can use this in a foreach later. Is it possible?
I've tried everything without success.
You can loop through every elements in your associative array and create an empty array before the loop. Then push the key as well as value to the new array. This way you can get that desirable output:
<?php
$assocArray = [
"apple" => "one",
"orange" => "two",
"strawberry" => "three"
];
echo "Before: \n\r";
print_r($assocArray);
$newArray = [];
foreach ($assocArray as $key => $item) {
$newArray[] = [$key, $item];
}
echo "After: \n\r";
print_r($newArray);
Output
Before:
Array
(
[apple] => one
[orange] => two
[strawberry] => three
)
After:
Array
(
[0] => Array
(
[0] => apple
[1] => one
)
[1] => Array
(
[0] => orange
[1] => two
)
[2] => Array
(
[0] => strawberry
[1] => three
)
)
``
You can also do this:
$array = [
'apple' => 'one',
'orange' => 'two',
'strawberry' => 'three',
];
$output = array_map(function($k, $v) {
return [$k, $v];
}, array_keys($array), $array);
var_dump($output);
I have two array as bellow :
First array :
Array
(
[0] => Array
(
[0] => Array
(
[name] => one
[number] => 051
)
[1] => Array
(
[name] => two
[number] => 052
)
[2] => Array
(
[name] => three
[number] => 053
)
)
[1] => Array
(
[0] => Array
(
[name] => four
[number] => 061
)
[1] => Array
(
[name] => five
[number] => 062
)
)
)
I want to make output from first array above
[0] => 051, 052, 053.
[1] => 061, 062.
Array
(
[0] => Array
(
[0] => Array
(
[name] => book
[number] => 41
)
[1] => Array
(
[name] => pencil
[number] => 42
)
)
[1] => Array
(
[name] => eraser
[number] => 71
)
)
I want to make output from second array above
[0] => 41, 42.
[1] => 71.
Please advise. Thank you.
You can make a try like this way with two foreach() loop.
$numbers = [];
foreach ($array as $k => $v) {
$num = [];
foreach ($v as $k2 => $v2) {
$num[] = $v2['number'];
}
$numbers[$k] = implode(',',$num).'.';
}
print_r($numbers);
DEMO: https://3v4l.org/mEeO7
you can try something like this
$arr = Array (
Array (
Array (
"name" => "one",
"number" => "051"
),
Array (
"name" => "two",
"number" => "052"
),
Array (
"name" => "three",
"number" => "053"
)
),
Array (
Array (
"name" => "four",
"number" => "061"
),
Array (
"name" => "five",
"number" => "062"
)
)
);
foreach ($arr as $k => $s_arr) {
echo "[" . $k . "] => ";
foreach ($s_arr as $k2 => $v2) {
echo $v2["number"] . " ";
}
echo "\n";
}
I need to create an dynamic array and can't get it right.
I need something like this:
Product Name
top
White
Black
Bottom
Red
Green
I came up with this code, which is producing the above text, so my logic must be about right.
$set = array();
$set['name'] = "Product Name";
$options = array("top", "bottom");
$values['top'] = array("White", "Black");
$values['bottom'] = array("Red", "Green");
echo "<pre>".$set['name']."</pre>";
foreach ($options as $o) {
echo "<pre>- $o</pre>";
$set['options'][]['name'] = $o;
foreach ($values[$o] as $v) {
echo "<pre>-- $v</pre>";
$set['options'][]['values']['name'] = $v;
}
}
The array that is created with the above code is:
Array
(
[name] => Product Name
[options] => Array
(
[0] => Array
(
[name] => top
)
[1] => Array
(
[values] => Array
(
[name] => White
)
)
[2] => Array
(
[values] => Array
(
[name] => Black
)
)
[3] => Array
(
[name] => bottom
)
[4] => Array
(
[values] => Array
(
[name] => Red
)
)
[5] => Array
(
[values] => Array
(
[name] => Green
)
)
)
)
THe output I want is:
Array
(
[name] => Product Name
[options] => Array
(
[0] => Array
(
[name] => top
[values] => Array
(
[0] => Array
(
[name] => White
)
[1] => Array
(
[name] => Black
)
)
)
[1] => Array
(
[name] => bottom
[values] => Array
(
[0] => Array
(
[name] => Red
)
[1] => Array
(
[name] => Green
)
)
)
)
)
What am I missing ?
You got
$set['options'][]['name'] = $o;
^
this one
and
$set['options'][]['values']['name'] = $v;
^
This one
in outer as well as inner loop which was adding new item to array (so there were indexes like 0, 1, 2 ...) so could not produce what you wanted.
You may correct your array like below:
Demo
<?php
$set = array();
$set['name'] = "Product Name";
$options = array("top", "bottom");
$values['top'] = array("White", "Black");
$values['bottom'] = array("Red", "Green");
echo "<pre>".$set['name']."</pre>";
foreach ($options as $o) {
echo "<pre>- $o</pre>";
$vals = array();
foreach ($values[$o] as $v) {
echo "<pre>-- $v</pre>";
$vals[] = array('name' => $v );
}
$set['options'][] = array('name' => $o, 'values' => $vals );
}
print_r($set);
?>
I have an array ($myArray) which looks like
Array ( [0] =>
Array ( [0] =>
Array (
[Date] => 1776-08-08
[Color] => Yellow
[Description] => Rotten
) )
[1] => Array ( )
[2] =>
Array ([0] =>
Array (
[Date] => 2018-05-13
[Color] => Red
[Status] => Fresh
)
[1] =>
Array (
[Date] => 1991-03-29
[Color] => Green
[Status] => Fresh ) )
I loop though the content for the values of Date using
array_walk_recursive($myArray, function($v, $k){
if ($k == "Date") echo $v . PHP_EOL;
This would get me the correct output.
1776-08-08 2018-05-13 1991-03-29
I want to add the output into an array and even if the value is null (ie[1] above) to still set an empty array.
For example $newArray =
Array ( [0] => 1776-08-08 )
Array ( )
Array ( [0] => 2018-05-13 [1] => 1991-03-29 )
Given your example, an option is to use array_column() on each of the items in the outermost array, which is easy with the array_map() function.
$input = array(
array(
array(
"Date" => "1776-08-08",
"Color" => "Yellow",
"Description" => "Rotten",
),
),
array(
),
array(
array(
"Date" => "2018-05-13",
"Color" => "Red",
"Status" => "Fresh",
),
array(
"Date" => "1991-03-29",
"Color" => "Green",
"Status" => "Fresh",
),
),
);
$output = array_map(function($sub_arrays) {
return array_column($sub_arrays, "Date");
}, $input);
print_r($output);
The above will output something like:
Array
(
[0] => Array
(
[0] => 1776-08-08
)
[1] => Array
(
)
[2] => Array
(
[0] => 2018-05-13
[1] => 1991-03-29
)
)
You'll need to do a normal foreach loop for the top-level, and then use array_walk_recursive for the nested arrays.
$newArray = array();
foreach ($myArray as $el) {
$temp = array();
array_walk_recursive($el, function($v, $k) use (&$temp) {
if ($k == "Date") {
$temp[] = $v;
}
});
$newArray[] = $temp;
}
DEMO
I have array1 like this:
Array
(
[0] => 123
[1] => 456
[2] => 789
)
And array 2 like this
Array
(
[0] => Array
(
[0] => some text
[1] => 888
[2] => some
[3] => text
)
[1] => Array
(
[0] => some text
[1] => 123
[2] => some
[3] => text
)
[2] => Array
(
[0] => some text
[1] => 999
[2] => some
[3] => text
)
[3] => Array
(
[0] => some text
[1] => Array
(
[1] => 456
[2] => 789
)
[2] => some
[3] => text
)
[4] => Array
(
[0] => some text
[1] => 123
[2] => some
[3] => text
)
)
I am checking only 1. column of second array and finding values that match values from first array. This is my code:
$test=array();
$xcol = array_column($array2, 1);
foreach( $array1 as $key => $value ) {
if( ($foundKey = array_keys($xcol, $value)) !== false ) {
$rrt=$foundKey;
foreach($rrt as $rte){
$test[]=$array2[$rte];
}
}
}
echo "<pre>";
print_r($test);
echo "</pre>";
It is working and giving me proper results but it does not check for all levels. Can anybody please point me what am I doing wrong?
My output is:
Array
(
[0] => Array
(
[0] => some text
[1] => 123
[2] => some
[3] => text
)
[1] => Array
(
[0] => some text
[1] => 123
[2] => some
[3] => text
)
)
And desired output is:
Array
(
[0] => Array
(
[0] => some text
[1] => 123
[2] => some
[3] => text
)
[1] => Array
(
[0] => some text
[1] => Array
(
[1] => 456
[2] => 789
)
[2] => some
[3] => text
)
[2] => Array
(
[0] => some text
[1] => 123
[2] => some
[3] => text
)
)
Solution:
create a loop which loop your $array2 which holds datas you wanted to get whos values match in first array $array1
foreach($array2 as $data) {
Inside loop create another loop which loop your indexes
foreach($data as $value) {
But before than create a condition if your index value is array loop it and check it it's index value is match in any indexes from $array1 use php function in_array for that
if (gettype($value) == "array") {
foreach($value as $val) {
if (in_array($val, $array1) ) {
$result[] = $data;
Then if you find it just stop the loop using break to avoid duplication
break;
}
}
Else you just directly use in_array
} else if (in_array($value, $array1)) {
$result[] = $data;
}
}
}
Just grab the code here in Demo
Help it helps just mark it answer if you are satisfied to it
Lets make a recursive method for this. What is recursion you ask? Well it is simply a method that calls it self.
<?php
$array1 = array(123,456,789);
$array2 = array(
array(
"some text"
, 888
, "some"
, "text"
),
array(
"some text"
,123
,"some"
,"text"
),
array(
"some text"
,999
,"some"
,"text"
),
array(
"some text"
,array(456,789)
,"some"
,"text"
),
array(
"another text"
,123
,"some"
,"text"
)
);
$final = array();
foreach($array1 as $needle){
foreach($array2 as $haystack){
if(find($needle,$haystack)){
$final[] = $haystack;
}
}
}
print_r($final);
function find($needle, $haystack){
$result = false;
foreach ($haystack as $value) {
if(is_array($value)){
$result = find($needle, $value);
} else {
if($needle == $value){
$result = true;
}
}
}
return $result;
}