How to get random array from multidimensional array in php? - php

I am trying to get random array:
for example:
$navItems= array(
array(
slug => "top10.php",
title => "Top 10 geriausi"
),
array(
slug => "index.php",
title => "Pagrindinis"
),
array(
slug => "top-prasciausi.php",
title => "Top 10 prasciausi"
),
array(
slug => "visi-politikai.php",
title => "Visi politiki"
),
);
and after that use this random array inside foreach loop like this:
foreach ($navItems as $item) {
echo "<li class=\"hov\">$item[title]<li>";
}
Instead of $navItems variable i want to use that random arrays from $navItems array

You can use shuffle to randomize the array
Note: If you are trying to pick one from the array, you can use shuffle and select the first element of the array.
$navItems = array(
array(
'slug' => "top10.php",
'title' => "Top 10 geriausi"
),
array(
'slug' => "index.php",
'title' => "Pagrindinis"
),
array(
'slug' => "top-prasciausi.php",
'title' => "Top 10 prasciausi"
),
array(
'slug' => "visi-politikai.php",
'title' => "Visi politiki"
),
);
shuffle( $navItems );
echo "<pre>";
print_r( $navItems );
echo "</pre>";
This will result to something like:
Array
(
[0] => Array
(
[slug] => index.php
[title] => Pagrindinis
)
[1] => Array
(
[slug] => visi-politikai.php
[title] => Visi politiki
)
[2] => Array
(
[slug] => top10.php
[title] => Top 10 geriausi
)
[3] => Array
(
[slug] => top-prasciausi.php
[title] => Top 10 prasciausi
)
)
Doc: shuffle

Related

How to change array format in PHP?

I have simple question:
How to change this array
Array ( [0] => taxonomy-term-1 [1] => taxonomy-term-2 [2] => taxonomy-term-3 )
to this array?
array( 'taxonomy-term-1', 'taxonomy-term-2', 'taxonomy-term-3' )
Why I am asking for that:
$slugs = wp_get_post_terms($post->ID,'ml_patternsCustomTaxonomy',['fields'=>'slugs']);
returns
Array ( [0] => taxonomy-term-1 [1] => taxonomy-term-2 [2] => taxonomy-term-3 )
but
array(
'title' => $title,
'content' => trim($patternContent),
'categories' => $slugs
)
doesn't work, and
//
'categories' => array( 'taxonomy-term-1', 'taxonomy-term-2', 'taxonomy-term-3' ),
works.
Why?

convert indexed multidimensional array to associative multidimensional array

I have an indexed array with nested categories like this:
array (
0 =>
array (
'name' => 'Furniture',
'id' => 'b3cdd1k',
'content' =>
array (
0 =>
array (
'name' => 'Tables',
'id' => 'nw24ga3',
'content' =>
array (
0 =>
array (
'name' => 'Wooden tables',
'id' => 'ba5lgaz',
),
1 =>
array (
'name' => 'Glass tables',
'id' => 'rqt91gz',
),
),
),
),
),
1 =>
array (
'name' => 'Lamps',
'id' => 'vb1a4nf',
),
2 =>
array (
'name' => 'Doors',
'id' => 'a5l4gal',
'content' =>
array (
0 =>
array (
'name' => 'Entrance doors',
'id' => 'qwg30fb',
),
),
),
)
Is there elegant way to convert it to associative array (where keys are id's) and keep nesting structure?
After conversion I excepting something like this:
array (
'b3cdd1k' =>
array (
'name' => 'Furniture',
'content' =>
array (
'nw24ga3' =>
array (
'name' => 'Tables',
'content' =>
array (
'ba5lgaz' =>
array (
'name' => 'Wooden tables',
),
'rqt91gz' =>
array (
'name' => 'Glass tables',
),
),
),
),
),
'vb1a4nf' =>
array (
'name' => 'Lamps',
),
'a5l4gal' =>
array (
'name' => 'Doors',
'content' =>
array (
'qwg30fb' =>
array (
'name' => 'Entrance doors',
),
),
),
)
You could try this - not the most elegant, but seems to work:
function convert(&$a)
{
$result = Array();
foreach($a as $k=>&$v)
{
$result[$v['id']]['name'] = $v['name'];
if(is_array($v['content'])) $result[$v['id']]['content'] = convert($v['content']);
}
return $result;
}
if(count($array) != 0) $result = convert($array);

PHP loop trying to repeat array string with codeigniter

I have this code running in CodeIgniter:
for($j=0;$j<count($detials);$j++){
$newValues = array(
'Description' => $detials[$j]
);
}
currently it outputs this:
Array ( [Description] => Array ( [0] => this [1] => is [2] => a [3] => name ) )
I want the loop to create this:
$newValues = array(
'Description' => $detials[1]
'Description' => $detials[2]
'Description' => $detials[3]
);
How do I do this?
I am not sure, but I think you may be trying to do this:
for($j=0;$j<count($detials);$j++){
$newValues[] = array('Description' => $detials[$j]);
}
Which would give you this array:
$newValues = array(
array('Description' => $detials[1]),
array('Description' => $detials[2]),
array('Description' => $detials[3])
);

PHP Replace Array Values

I have 2 multidimensional arrays that I am working with:
$arr1 =
Array
([type] => characters
[version] => 5.6.7.8
[data] => Array
([Char1] => Array
([id] => 1
[name] =>Char1
[title] =>Example
[tags] => Array
([0] => DPS
[1] => Support))
[Char2] => Array
([id] => 2
[name] =>Char2
[title] =>Example
[tags] => Array
([0] => Tank
[1] => N/A)
)
)
etc...
$arr2=
Array
([games] => Array
([gameId] => 123
[gameType => Match
[char_id] => 1
[stats] => Array
([damage] => 55555
[kills] => 5)
)
([gameId] => 157
[gameType => Match
[char_id] => 2
[stats] => Array
([damage] => 12642
[kills] => 9)
)
etc...
Basically, I need almost all the data in $arr2... but only the Char name from $arr1. How could I merge or add the $arr1['name'] key=>value into $arr2 where $arr1['id'] is equal to $arr2['char_id'] as the "id" field of each array is the same number.
I've attempted using array_merge and array_replace, but I haven't come up with any working solutions. This is also all data that I am receiving from a 3rd party, so I have no control on initial array setup.
Thanks for any help or suggestions!
Actually, this is quite straighforward. (I don't think there a built-in function that does this.)
Loop $arr2 and under it loop also $arr1. While under loop, just add a condition that if both ID's match, add that particular name to $arr2. (And use some referencing & on $arr2)
Consider this example:
// your data
$arr1 = array(
'type' => 'characters',
'version' => '5.6.7.8',
'data' => array(
'Char1' => array(
'id' => 1,
'name' => 'Char1',
'title' => 'Example',
'tags' => array('DPS', 'Support'),
),
'Char2' => array(
'id' => 2,
'name' => 'Char2',
'title' => 'Example',
'tags' => array('Tank', 'N/A'),
),
),
);
$arr2 = array(
'games' => array(
array(
'gameId' => 123,
'gameType' => 'Match',
'char_id' => 1,
'stats' => array('damage' => 55555, 'kills' => 5),
),
array(
'gameId' => 157,
'gameType' => 'Match',
'char_id' => 2,
'stats' => array('damage' => 12642, 'kills' => 9),
),
),
);
foreach($arr2['games'] as &$value) {
$arr2_char_id = $value['char_id'];
// loop and check against the $arr1
foreach($arr1['data'] as $element) {
if($arr2_char_id == $element['id']) {
$value['name'] = $element['name'];
}
}
}
echo '<pre>';
print_r($arr2);
$arr2 should look now like this:
Array
(
[games] => Array
(
[0] => Array
(
[gameId] => 123
[gameType] => Match
[char_id] => 1
[stats] => Array
(
[damage] => 55555
[kills] => 5
)
[name] => Char1 // <-- name
)
[1] => Array
(
[gameId] => 157
[gameType] => Match
[char_id] => 2
[stats] => Array
(
[damage] => 12642
[kills] => 9
)
[name] => Char2 // <-- name
)
)
)
Iterate over $arr2 and add the data to it from the matching $arr1 array value:
$i = 0;
foreach($arr2['games'] as $arr2Game){
$id = $arr2Game['char_id'];
$arr2['games'][$i]['name'] = $arr1['data'][$id]['name'];
$i++;
}
Have not tested this code.
If I'm understanding you correctly, you want to add a name index to each of the arrays within the $arr2['games'] array.
foreach($arr2['games'] as $key => $innerArray)
{
$arr2['games'][$key]['name'] = $arr1['data']['Char'.$innerArray['char_id']]['name'];
}

How do I make a list of specific values in a PHP multidimensional array?

I have a multidimensional array like so:
$neighborhood => array(
'the_smiths' => array(
'dad' => 'Donald',
'mom' => 'Mary',
'daughter' => 'Donna',
'son' => 'Samuel'
)
'the_acostas' => array(
'dad' => 'Diego',
'mom' => 'Marcela',
'daughter' => 'Dominga',
'son' => 'Sergio'
)
);
I would like to create another array (let's call it $array_of_moms) of all the moms in the neighborhood. Pulling them all in separately is doable, but not practical (like so):
$array_of_moms = array(
$neighborhood['the_smiths']['mom'],
$neighborhood['the_acostas']['mom'],
)
How do I create something like this:
$array_of_moms = $neighborhood['mom'];
$moms = array();
foreach($neighborhood as $family)
{
$moms[] = $family['mom'];
}
This'll iterate through each family in the array and add the mom to the new $moms array.
Using foreach, you can iterate through an array with variable indicies.
$array_of_moms = array();
foreach ($neighborhood AS $family) {
$array_of_moms[] = $family['mom']; // append mom of each family to array
}
If you can manipulate your array, you could:
<?php
$neighborhood = array(
'families' => array(
'the_smiths' => array(
'dad' => 'Donald',
'mom' => 'Mary',
'daughter' => 'Donna',
'son' => 'Samuel'
),
'the_acostas' => array(
'dad' => 'Diego',
'mom' => 'Marcela',
'daughter' => 'Dominga',
'son' => 'Sergio'
)
)
);
foreach ($neighborhood['families'] as $family => $folks) {
$neighborhood['moms'][] = $folks['mom'];
}
print_r($neighborhood);
?>
Which outputs:
Array
(
[families] => Array
(
[the_smiths] => Array
(
[dad] => Donald
[mom] => Mary
[daughter] => Donna
[son] => Samuel
)
[the_acostas] => Array
(
[dad] => Diego
[mom] => Marcela
[daughter] => Dominga
[son] => Sergio
)
)
[moms] => Array
(
[0] => Mary
[1] => Marcela
)
)
http://codepad.org/xbnj5UmV

Categories