I have an array that looks like this,
[0] => Array
(
[youtube_showreel_url_1] => youtube1.com
[youtube_showreel_description] => youtube1.com - desc
)
[1] => Array
(
[youtube_showreel_url_2] => youtube2.com
[youtube_showreel_description] => youtub2.com - desc
)
[2] => Array
(
[youtube_showreel_url_3] => youtube3.com
[youtube_showreel_description] => youtube3.com - desc
)
[3] => Array
(
[youtube_showreel_url_4] => youtube4.com
[youtube_showreel_description] => youtube4.com - desc
)
[4] => Array
(
[youtube_showreel_url_5] => youtube5.com
[youtube_showreel_description] => youtube5.com - desc
)
Is it possible with PHP to turn it into something that looks like this?
[0] => Array (
[youtube_showreel_url_1] => youtube1.com
[youtube_showreel_description] => youtube1.com - desc
[youtube_showreel_url_2] => youtube2.com
[youtube_showreel_description] => youtub2.com - desc
[youtube_showreel_url_3] => youtube3.com
[youtube_showreel_description] => youtube3.com - desc
[youtube_showreel_url_4] => youtube4.com
[youtube_showreel_description] => youtube4.com - desc
[youtube_showreel_url_5] => youtube5.com
[youtube_showreel_description] => youtube5.com - desc
)
Can explode it or run it through a loop or something?
Assuming your original data is held in a variable called $input:
// This will hold the result
$result = array();
foreach ($input as $index => $item) { // Loop outer array
foreach ($item as $key => $val) { // Loop inner items
$result[$key] = $val; // Create entry in $result
}
}
// Show the result
print_r($result);
However, your input has the same key appearing in it more than once, and the later values will overwrite the first one. So you might want to do something like this:
foreach ($input as $index => $item) { // Loop outer array
foreach ($item as $key => $val) { // Loop inner items
$result[$key.$index] = $val; // Create entry in $result
}
}
<?php
$aNonFlat = array(
1,
2,
array(
3,
4,
5,
array(
6,
7
),
8,
9,
),
10,
11
);
$objTmp = (object) array('aFlat' => array());
array_walk_recursive($aNonFlat, create_function('&$v, $k, &$t', '$t->aFlat[] = $v;'), $objTmp);
var_dump($objTmp->aFlat);
/*
array(11) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
[5]=>
int(6)
[6]=>
int(7)
[7]=>
int(8)
[8]=>
int(9)
[9]=>
int(10)
[10]=>
int(11)
}
*/
?>
source : http://ca.php.net/manual/fr/function.array-values.php#86784
array keys have to have unique names meaning that you wouldn't be able to have multiple youtube_showreel_description keys or else you just overwrite the value. You could rename the key to something like youtube_showreel_description_NN where NN is the number of the description similar to how you have the url.
there would be a collision on "youtube_showreel_description"
you should think of a better data structure for that
I would suggest you retain its multidimensional array structure
Array
(
[0] => Array
(
[url] => youtube1.com
[description] => youtube1.com - desc
)
[1] => Array
(
[url] => youtube2.com
[description] => youtube2.com - desc
)
[2] => Array
(
[url] => youtube3.com
[description] => youtube3.com - desc
)
[3] => Array
(
[url] => youtube1.com
[description] => youtube3.com - desc
)
[4] => Array
(
[url] => youtube1.com
[description] => youtube4.com - desc
)
)
If you like it that way, I think its cleaner, and easier to parse
Not exactly, the [youtube_showreel_description] would overwrite itself after each declaration, you would need to use a unique identifier for each key. If the keys are not important you can just loop through your existing array with a foreach loop and set a regular iterative key and "know" that the array comes in sets where the first element is the url and the second is the description.
Edit: If the keys aren't important, you could use the url as your key and description as your value, this will make it easier to iterate over as each element pertains to only one item.
As everyone has already mentioned, you're going to get a collision on 'youtube_showreel_description'. How about something like this?:
// values (stays the same - no need to reformat)
$values = array(
array(
'youtube_showreel_url_1' => 'youtube1.com',
'youtube_showreel_description' => 'youtube1.com desc'
),
array(
'youtube_showreel_url_2' => 'youtube2.com',
'youtube_showreel_description' => 'youtube2.com desc'
),
array(
'youtube_showreel_url_3' => 'youtube3.com',
'youtube_showreel_description' => 'youtube3.com desc'
),
array(
'youtube_showreel_url_4' => 'youtube4.com',
'youtube_showreel_description' => 'youtube4.com desc'
)
);
// the good stuff
$result = array_map(function($v) {
return array(
'url' => array_shift($v),
'description' => $v['youtube_showreel_description']
);
}, $values);
// result
array(4) {
[0]=>
array(2) {
["url"] => string(12) "youtube1.com"
["description"] => string(17) "youtube1.com desc"
}
[1]=>
array(2) {
["url"] => string(12) "youtube2.com"
["description"] => string(17) "youtube2.com desc"
}
[2]=>
array(2) {
["url"] => string(12) "youtube3.com"
["description"] => string(17) "youtube3.com desc"
}
[3]=>
array(2) {
["url"] => string(12) "youtube4.com"
["description"] => string(17) "youtube4.com desc"
}
}
Related
Supposed I have an array of
array(8) {
[0] =>
array(1) {
'Peter' =>
int(4)
}
[1] =>
array(1) {
'Piper' =>
int(4)
}
[2] =>
array(1) {
'picked' =>
int(4)
}
[3] =>
array(1) {
'peck' =>
int(4)
}
[4] =>
array(1) {
'pickled' =>
int(4)
}
How can I sort this multidimentional array by key example (Peter). I tried using
ksort($arr);
but it just return a boolean
The output that I want
array(8) {
[0] =>
array(1) {
'peck' =>
int(4)
}
[1] =>
array(1) {
'Peter' =>
int(4)
}
[2] =>
array(1) {
'picked' =>
int(4)
}
[3] =>
array(1) {
'pickled' =>
int(4)
}
[4] =>
array(1) {
'piper' =>
int(4)
}
the array should be sorted by key and in ascending order.
Sort with usort like this, check the demo
usort($array,function($a,$b){
return strcmp(strtolower(key($a)),strtolower(key($b)));
});
The ksort() method does an in-place sort. So while it only returns a boolean (as you correctly state), it mutates the values inside $arr to be in the sorted order. Note that based on your expected output, it looks like you want to do a case insensitive search. For that, you need to use the SORT_FLAG_CASE sort flag. So, instead of calling ksort($arr), you instead want to use ksort($arr, SORT_FLAG_CASE). You can see how ksort() uses sort flags, in the sort() method's documentation. Hope that helps!
You can do something like this,
$temp = array_map(function($a){
return key($a); // fetching all the keys
}, $arr);
natcasesort($temp); // sorting values case insensitive
$result = [];
// logic of sorting by other array
foreach($temp as $v){
foreach($arr as $v1){
if($v == key($v1)){
$result[] = $v1;
break;
}
}
}
Demo
Output
Array
(
[0] => Array
(
[peck] => 4
)
[1] => Array
(
[Peter] => 4
)
[2] => Array
(
[picked] => 4
)
[3] => Array
(
[pickled] => 4
)
[4] => Array
(
[Piper] => 4
)
)
My code contains an array that has 7 elements and each element has a total number of different characters. I want, when the number of characters meets the criteria (<= 6) then create a new array.
The output is expected in the form of a two dimension array,
// My Variables, $value & $count
$value=array('as','fix','fine','is','port','none','hi','of');
for ($i=0; $i <count($value) ; $i++) {
$count[]=strlen($value[$i]);
}
Then have output like a,
// $value,
Array
(
[0] => as
[1] => fix
[2] => fine
[3] => is
[4] => port
[5] => none
[6] => hi
[7] => of
)
// Count the length $value and store in Variable $count,
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 2
[4] => 4
[5] => 4
[6] => 2
[7] => 2
)
and then I hope my code can produce output like this:
(Explode element where length <= 6)
// If length value in the variable $count,
Array
(
[0] => 2
[1] => 3
Total Length(5)
[2] => 4
[3] => 2
Total Length(6)
[4] => 4
Total Length(4)
[5] => 4
Total Length(4)
[6] => 2
[7] => 2
Total Length(4)
)
This is my question point:
// $Values RESULT
Array
(
[0] => Array
(
[0] => as
[1] => fix
)
[1] => Array
(
[0] => fine
[1] => is
)
[1] => Array
(
[0] => port
)
[1] => Array
(
[0] => none
)
[1] => Array
(
[0] => hi
[1] => of
)
)
Your examples were a bit hard to follow but here's what I've got:
<?php
$value=array('as','fix','fine','is','port','none','hi','of');
$final = [];
$accumulator = 0;
$final[0] = [];
$x = 0;
for ($i=0; $i <count($value) ; $i++) {
var_dump($accumulator);
if($accumulator + strlen($value[$i]) > 6) {
echo "adding ".$value[$i] ." to new\n\n";
$x++;
$final[$x] = [];
array_push($final[$x], $value[$i]);
$accumulator = strlen($value[$i]);
}else{
echo "adding ".$value[$i] . " to existing\n\n";
array_push($final[$x], $value[$i]);
$accumulator += strlen($value[$i]);
}
}
var_dump($final);
Yields
int(0)
adding as to existing
int(2)
adding fix to existing
int(5)
adding fine to new
int(4)
adding is to existing
int(6)
adding port to new
int(4)
adding none to new
int(4)
adding hi to existing
int(6)
adding of to new
array(5) {
[0]=>
array(2) {
[0]=>
string(2) "as"
[1]=>
string(3) "fix"
}
[1]=>
array(2) {
[0]=>
string(4) "fine"
[1]=>
string(2) "is"
}
[2]=>
array(1) {
[0]=>
string(4) "port"
}
[3]=>
array(2) {
[0]=>
string(4) "none"
[1]=>
string(2) "hi"
}
[4]=>
array(1) {
[0]=>
string(2) "of"
}
}
http://sandbox.onlinephpfunctions.com/code/20a63b83ad5524c5cd77e111bc15e197bf8bfba2
I have looked at numerous threads relating to this and none of them have been of any help to me. I have an array which follows the basic structure of $array[location][store][person] = funds. What is the most efficient way of sorting the array so that the [person] key is in ASC order?
This is what it looks like now:
Array
(
[Florida] => Array
(
[AppleSauce] => Array
(
[Rabbit, Hunting] => 5
[Brown, Bubba] => 20
[Chicken, Cantina] => 10
[Gum, Bubble] => 10
[Pool, Swimming] => 4
[Bath, Taka] => 2
)
)
[Texas] => Array
(
[BeatleJuice] => Array
(
[Chicken, Cantina] => 10
[Pool, Swimming] => 4
[House, Road] => 5
)
[CaramelApple] => Array
(
[Chicken, Cantina] => 10
[Pool, Swimming] => 4
[House, Road] => 5
)
)
This is what I am looking for:
Array
(
[Florida] => Array
(
[AppleSauce] => Array
(
[Bath, Taka] => 2
[Brown, Bubba] => 20
[Chicken, Cantina] => 10
[Gum, Bubble] => 10
[Pool, Swimming] => 4
[Rabbit, Hunting] => 5
)
)
[Texas] => Array
(
[BeatleJuice] => Array
(
[Chicken, Cantina] => 10
[House, Road] => 5
[Pool, Swimming] => 4
)
[CaramelApple] => Array
(
[Chicken, Cantina] => 10
[House, Road] => 5
[Pool, Swimming] => 4
)
)
You can use ksort to sort the array keys of people in alphabetical order
foreach($array as $state => $locations) {
foreach($locations as $location => $people) {
ksort($array[$state][$location]);
}
}
The php function array_multisort can do this:
Sorting multi-dimensional array
<?php
$ar = array(
array("10", 11, 100, 100, "a"),
array( 1, 2, "2", 3, 1)
);
array_multisort($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>
In this example, after sorting, the first array will transform to "10", 100, 100, 11, "a" (it was sorted as strings in ascending order). The second will contain 1, 3, "2", 2, 1 (sorted as numbers, in descending order).
array(2) {
[0]=> array(5) {
[0]=> string(2) "10"
[1]=> int(100)
[2]=> int(100)
[3]=> int(11)
[4]=> string(1) "a"
}
[1]=> array(5) {
[0]=> int(1)
[1]=> int(3)
[2]=> string(1) "2"
[3]=> int(2)
[4]=> int(1)
}
}
This is based from the official documentation:
http://php.net/manual/en/function.array-multisort.php
I have an array of objects, but I need to remove a similar objects by a few properties from them:
for example:
array(12) {
[0]=>
object(stdClass)#848 (5) {
["variant"]=>
object(stdClass)#849 (4) {
["name"]=>
string(8) "Alex"
}
["age"]=>
int(10)
}
[1]=>
object(stdClass)#851 (5) {
["variant"]=>
object(stdClass)#852 (4) {
["name"]=>
string(8) "Alex"
}
["age"]=>
int(10)
}
How to make a one object in array for this ( if for example I need to compare only by a name property? )
Still have an issue with it.
Updated
I've create a new array of objects:
$objects = array(
(object)array('name'=>'Stiven','age'=>25,'variant'=>(object)array('surname'=>'Sigal')),
(object)array('name'=>'Michael','age'=>30,'variant'=>(object)array('surname'=>'Jackson')),
(object)array('name'=>'Brad','age'=>35,'variant'=>(object)array('surname'=>'Pit')),
(object)array('name'=>'Jolie','age'=>35,'variant'=>(object)array('surname'=>'Pit')),
);
echo "<pre>";
print_r($objects);
So what I need to do is to compare an object properties (variant->surnames and ages), if two objects has a similar age and variant->surname we need to remove the one of these objects.
A half of solution is:
$tmp = array();
foreach ($objects as $item=>$object)
{
$tmp[$object->variant->surname][$object->age] = $object;
}
print_r($tmp);
Unfortunatelly I need an old-style array of objects.
I've found an example.
<?php
$a = array (
0 => array ( 'value' => 'America', ),
1 => array ( 'value' => 'England', ),
2 => array ( 'value' => 'Australia', ),
3 => array ( 'value' => 'America', ),
4 => array ( 'value' => 'England', ),
5 => array ( 'value' => 'Canada', ),
);
$tmp = array ();
foreach ($a as $row)
if (!in_array($row,$tmp)) array_push($tmp,$row);
print_r ($tmp);
?>
Quoted from here
This is pretty basic, but my question is:
Given an array:
$a = array(
0 => array('Rate'=> array('type_id'=>1, 'name' => 'Rate_1', 'type'=>'day','value'=>10)),
1 => array('Rate'=> array('type_id'=>1, 'name' => 'Rate_2', 'type'=>'night','value'=>8)),
2 => array('Rate'=> array('type_id'=>2, 'name' => 'Rate_3', 'type'=>'day','value'=>7)),
3 => array('Rate'=> array('type_id'=>2, 'name' => 'Rate_4', 'type'=>'nigh','value'=>16)),
4 => array('Rate'=> array('type_id'=>3, 'name' => 'Rate_5', 'type'=>'day','value'=>10))
);
What is the most efficient way to change it so we have something like:
$new_array = array(
[type_id] => array(
[type] => array(
[value]
)
)
)
);
In other words, I would like to strip some data (the name, which I don't need) and reorganise the dimensions of the array. In the end I would have an array which I would be able to access the values by $new_array['type_id']['type']['value'].
Not entirely sure if this is exactly what you want, but with this you can access the values by saying
echo $new[TYPE_ID][DAY_OR_NIGHT];
$new = array();
foreach($a AS $b){
$c = $b['Rate'];
$new[$c['type_id']][$c['type']] = $c['value'];
}
Using print_r on $new would give you:
Array
(
[1] => Array
(
[day] => 10
[night] => 8
)
[2] => Array
(
[day] => 7
[night] => 16
)
[3] => Array
(
[day] => 10
)
)
Since php 5.3.0, array_reduce() allows using an array as the initial value, given your initial array $a, you can use the following code
function my_reducer ($result, $item) {
$result[$item['Rate']['type_id']][$item['Rate']['type']] = $item['Rate']['value'];
return $result;
}
$assoc_arr = array_reduce($a, 'my_reducer', array());
var_dump($assoc_arr);
This returns
array(3) { [1]=> array(2) {
["day"]=>
int(10)
["night"]=>
int(8) } [2]=> array(2) {
["day"]=>
int(7)
["nigh"]=>
int(16) } [3]=> array(1) {
["day"]=>
int(10) } }