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
)
)
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 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 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 :)
Trying to alphabetically merge arrays with foreach loop.
<?php
$fruits = array(
'Apple' => array('ids'=>array(1,2)),
'Banana' => array('ids'=>array(3,4)),
'Ananas' => array('ids'=>array(5,6))
);
$result = array();
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
$result[$first_letter] = $subarr;
}
print_r($result);
gives me smth like
Array
(
[A] => Array
(
[ids] => Array
(
[0] => 5
[1] => 6
)
)
[B] => Array
(
[ids] => Array
(
[0] => 3
[1] => 4
)
)
)
instead of smth like
[A] => Array
(
[ids] => Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 6
)
)
how can I fix it?
You overwrite your result every iteration in this line:
$result[$first_letter] = $subarr;
Just create a new array if the subArray in the result array doesn't exists and merge the ids subArray into your result array.
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
if(!isset($result[$first_letter]))
$result[$first_letter] = [];
$result[$first_letter] = array_merge($result[$first_letter], $subarr["ids"]);
}
Please try to use foreach loop.
$fruits = array(
'Apple' => array('ids'=>array(1,2)),
'Banana' => array('ids'=>array(3,4)),
'Ananas' => array('ids'=>array(5,6))
);
$result = array();
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
foreach($subarr as $key=>$value){
foreach ($value as $gkey => $gvalue) {
$result[$first_letter]['ids'][] = $gvalue;
}
}
}
echo "<pre>";
print_r($result);
Display above code output like below.
Array
(
[A] => Array
(
[ids] => Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 6
)
)
[B] => Array
(
[ids] => Array
(
[0] => 3
[1] => 4
)
)
)
Array
(
[0] => Array
(
[name] => WWW
)
[1] => Array
(
[name] => Hi
)
[2] => Array
(
[name] => Hello
)
[3] => Array
(
[name] => World
)
)
I have the above array and I want to count the number of keys.
When using the following code
$temp = array_keys($array);
echo $temp;
the result is 2 instead of 4 (0,1,2,3). What I'm doing wrong ?
You need to count the array in order to get a number:
$arr = array
(
"0" => array
(
"name" =>"WWW"
),
"1" => array
(
"name" => "Hi"
),
"2" => array
(
"name" => "Hello"
),
"3" => array
(
"name" => "World"
)
);
$keys_count = count($arr);
echo $keys_count;
Just count the array itself: count($array).
There's always the same amount of keys as there are values!
Very simple buddy. Look this:
$array = array(0 => 100, "color" => "red");
print_r(count($array));
php.net help you for all! ;)