How to create arrays for each values - PHP - php

// 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);

Related

Store column data from a multidimensional array while preserving missing elements

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

PHP: Modify array's elements sequence

I've an php array that I've got from Excel file, for example:
$arrayOne
Array
{
[0] => Array
{
[0] => 0_age
[1] => 1_academic id
[2] => 2_name
[3] => 3_sex
}
[1] => Array
{
[0] => 0_18
[1] => 1_110291
[2] => 2_Jason
[3] => 3_Male
}
}
and in the mid of proccess, the array value from index [0] that consist data from Excel Header set into duallistbox for elimination and sorting then set into new array $newArray. So then, I got this array result:
$newArray
Array
{
[0] => Array
{
[0] => 2_name
[1] => 1_academic id
[2] => 3_sex
}
}
And I expect the system also can eliminate and sorting the data of array from index [1] that consist of Excel Data.
So the expected result is like this:
$expectedArray
Array
{
[0] => Array
{
[0] => 2_Jason
[1] => 1_110291
[2] => 3_Male
}
}
anyone know idea or how to solve this case? I've add an id(e.g: 0_) in each of array value that maybe useful for sorting.
Thanks
Edited
The sorting is based on the id that have been set on each value of array, so each element on array index [1] from $arrayOne is reset into new sequence adapted to same id from $newArray.
One way
<?php
$arr = array(
array
(
"0_age",
"1_academic id",
"2_name",
"3_sex",
),
array
(
"0_18",
"1_110291",
"2_Jason",
"3_Male",
),
array
(
"0_28",
"1_110291111",
"2_Jason Second",
"3_Female",
)
);
?>
<pre>
<?php
$new_array = array();
$count = 0;
foreach($arr as $a){
if($count==0){
}else{
$new_array[] = array(
$arr[0][0] => $a[0],
$arr[0][1] => $a[1],
$arr[0][2] => $a[2],
$arr[0][3] => $a[3],
);
}
$count=$count+1;
}
print_r($new_array);
?>
</pre>
Output
Array
(
[0] => Array
(
[0_age] => 0_18
[1_academic id] => 1_110291
[2_name] => 2_Jason
[3_sex] => 3_Male
)
[1] => Array
(
[0_age] => 0_28
[1_academic id] => 1_110291111
[2_name] => 2_Jason Second
[3_sex] => 3_Female
)
)
Second Way that exactly match with your output
$new_array = array();
$count = 0;
foreach($arr as $a){
if($count==0){
}else{
$new_array[] = array(
//$a[0],
$a[2],
$a[1],
$a[3],
);
}
$count=$count+1;
}
print_r($new_array);
Output:
Array
(
[0] => Array
(
[0] => 0_18
[1] => 1_110291
[2] => 2_Jason
[3] => 3_Male
)
[1] => Array
(
[0] => 0_28
[1] => 1_110291111
[2] => 2_Jason Second
[3] => 3_Female
)
)
You can search, Sort and remove any field. Update on the basis of your requirement.

merging two array in php

I have two arrays
$arr1=Array
(
[0] => Array
(
[0] => 'a'
),
[1]=>Array
(
[0]=>'b'
),
[2] => Array
(
[0] => 'c'
),
[3]=>Array
(
[0]=>'d'
),
[4]=>Array
(
[0]=>'e'
)
);
$arr2=array('1','2');
output should be
$arr3=Array
(
[0] => Array
(
[0] => 'a',
[1]=>'1'
),
[1]=>Array
(
[0]=>'b',
[1]=>'2'
),
[2] => Array
(
[0] => 'c',
[1]=>'1'
),
[3]=>Array
(
[0]=>'d',
[1]=>'2'
),
[4]=>Array
(
[0]=>'e',
[1]=>'1'
)
);
can someone please suggest me some solutions
You can do this with a MultipleIterator and attach the first array as ArrayIterator and the second one as InfiniteIterator, e.g.
<?php
$arr1 = [["a"], ["b"], ["c"], ["d"], ["e"]];
$arr2 = [1,2];
$result = [];
$mIt = new MultipleIterator();
$mIt->attachIterator(new ArrayIterator($arr1));
$mIt->attachIterator(new InfiniteIterator(new ArrayIterator($arr2)));
foreach($mIt as $v)
$result[] = array_merge($v[0], [$v[1]]);
print_r($result);
?>
This version will allow $arr2 to contain any number of values, should that be a requirement:
<?php
$arr1 = [
['a'], ['b'], ['c'], ['d'], ['e'],
];
$arr2 = ['1', '2'];
// wrap the array in an ArrayIterator and then in an
// InfiniteIterator - this allows you to continually
// loop over the array for as long as necessary
$iterator = new InfiniteIterator(new ArrayIterator($arr2));
$iterator->rewind(); // start at the beginning
// loop over each element by reference
// push the current value in `$arr2` into
// each element etc.
foreach ($arr1 as &$subArray) {
$subArray[] = $iterator->current();
$iterator->next();
}
print_r($arr1);
This yields:
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
)
[2] => Array
(
[0] => c
[1] => 1
)
[3] => Array
(
[0] => d
[1] => 2
)
[4] => Array
(
[0] => e
[1] => 1
)
)
Hope this helps :)

PHP adding array onto array

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);
}

PHP Rearrange Array By Key

I have the following array...
Array (
["advertisers"] => Array (
...,
...,
...
),
["general"] => Array (
...,
...,
...
),
["publishers"] => Array (
...,
...,
...
)
)
I would like to rearrange the array so that "advertisers" comes first but "publishers" comes second and "general" is last.
Here :
<?
$fruits = Array(
"apples" => Array (
"one",
"two",
"three"
),
"oranges" => Array (
"one",
"two",
"three"
),
"bananas" => Array (
"one",
"two",
"three"
)
);
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>
Output:
apples = Array
bananas = Array
oranges = Array
You should use ksort. It sorts the array in alphabetic order by the array keys. Something like this
<?php
$arr = array('general'=>array(1,2,3), 'advertisers'=>array(7,8,9), 'publishers'=>array(11,12,13));
ksort($arr);
print '<pre>';
print_r($arr);
print '</pre>';
?>
Output
Array
(
[advertisers] => Array
(
[0] => 7
[1] => 8
[2] => 9
)
[general] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[publishers] => Array
(
[0] => 11
[1] => 12
[2] => 13
)
)

Categories