When i try to generate array, like this way:
$files_array = array(
'name' => array(),
'path' => array()
);
and then assign them values:
$files_array['name'][] = $fileinfo->getFilename();
$files_array['path'][] = $pathname;
the var_dump() function return me an array with numeric keys instead of "name" and "path", like this:
array(2) { [0]=> array(0) { } [1]=> array(0) { } }
What's wrong with the array? I've tried several way of doing this, but none give me my desired array.
EDIT:
A filled array dump, with the same code:
array(2) { [0]=> array(3) { [0]=> string(44) "./upload/4//lol/Nuovo documento di testo.TXT" [1]=> string(51) "./upload/4//lol/blue_bokeh_4-wallpaper-1366x768.jpg" [2]=> string(29) "./upload/4//lol/menny €.txt" } [1]=> array(3) { [0]=> string(28) "Nuovo documento di testo.TXT" [1]=> string(35) "blue_bokeh_4-wallpaper-1366x768.jpg" [2]=> string(13) "menny €.txt" } }
EDIT:
My full code
$files_array = array(
'name' => array(),
'path' => array()
);
$fileinfos = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootpath)
);
foreach ($fileinfos as $pathname => $fileinfo) {
if (!$fileinfo->isFile())
continue;
$files_array['name'][] = $fileinfo->getFilename();
$files_array['path'][] = $pathname;
}
That's how it works:
<?php
$files_array = array();
$i = 0;
$files_array[$i]['name'] = $fileinfo->getFilename();
$files_array[$i]['path'] = $pathname;
$i++;
The result output will be like:
array (size=2)
0 =>
array (size=2)
'name' => string 'file' (length=4)
'path' => string '/foo/' (length=5)
1 =>
array (size=2)
'name' => string 'file2' (length=5)
'path' => string '/bar/' (length=5)
I would suggest reading the php manual on arrays. Here are some examples.
//1
$files_array = array();
$files_array['name'] = array();
$files_array['path'] = array();
//2
$files_array = array(
'name' => [],
'path' => []
);
Related
I got output from one of the WordPress table cells. The following value is displayed.
$allcoinkey=get_option('_transient_mcw-custom-data');
var_dump($allcoinkey);
and the output:
[0]=>
array(2) {
["slug"]=>
string(7) "bitcoin"
["keywords"]=>
string(30) "بیتکوین,بیت کوین"
}
[1]=>
array(2) {
["slug"]=>
string(8) "ethereum"
["keywords"]=>
string(27) "اتریوم,اتاریوم"
}
}
How do I access keyword values where slug=bitcoin without foreach?
i use this sample code:
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
?>
You can do it like this:
<?php
$allcoinkey = [
[
'slug' => 'bitcoin',
'keywords' => 'بیتکوین,بیت کوین',
],
[
'slug' => 'ethereum',
'keywords' => 'اتریوم,اتاریوم',
],
];
$bitcoinKeywords = current(array_filter($allcoinkey, static function (array $cryptoCurrency) {
return $cryptoCurrency['slug'] === 'bitcoin';
}))['keywords'];
echo $bitcoinKeywords;
This question already has answers here:
array_combine is return only last value
(2 answers)
Closed 3 years ago.
I am using the following code to get two arrays into one json result. But getting only the first index as result. Is there any error in the code or anybody can suggest an alternate method to get the same result.
$array1 = $_POST['array1'];
$array2 = $_POST['array2'];
$jsonArray = array();
foreach (array_combine( $array1, $array2 ) as $name => $value) {
$jsonArray[] = array('name' => $name, 'value' => $value);
}
echo $json = json_encode($jsonArray);
$_POST['array1'] = array(4) {
[0]=>
string(3) "day1"
[1]=>
string(3) "day2"
[2]=>
string(3) "day3"
[3]=>
string(3) "day4"
}
$_POST['array2'] = array(4) {
[0]=>
string(3) "item1"
[1]=>
string(3) "item2"
[2]=>
string(3) "item3"
[3]=>
string(3) "item4"
}
Expected result should be like
[{"name":"day1","value":"item1"},{"name":"day2","value":"item2"},{"name":"day3","value":"item3"}]
Try this,
$arr1 = array('0' => 'day1', '1' => 'day2', '2' => 'day3', '3' => 'day4');
echo'<pre>';print_r($arr1);
$arr2 = array('0' => 'item1','1' => 'item2','2' => 'item3','3' => 'item4');
echo'<pre>';print_r($arr2);
echo'<pre>';print_r(array_combine($arr1, $arr2));
$newArray = array();
foreach(array_combine($arr1, $arr2) as $key => $value){
array_push($newArray, array('name'=> $key,'value'=>$value));
}
echo'<pre>';print_r($newArray);
echo json_encode($newArray);die;
I am trying to add values in a multidimensional array. Given below is how its supposed to be
array(
'name' => 'Hotel',
'placeholder' => 'Select the hotel',
'id' => $prefix . 'confirmation_hotel',
'type' => 'select_advanced',
'multiple' => false,
'options' => array(
'5896' => 'Hotel 1',
'6005' => 'Hotel 2'
)
),
But I getting data of options from a custom function with a foreach loop, given below is the code.
global $bookyourtravel_accommodation_helper, $bookyourtravel_car_rental_helper;
$items = $order->get_items();
$names = array();
foreach ( $items as $item_id => $item ) {
$bookyourtravel_theme_woocommerce = BookYourTravel_Theme_WooCommerce::get_instance();
$bookyourtravel_theme_woocommerce->init();
$order_names = $bookyourtravel_theme_woocommerce->order_item_name_confirmation($item);
}
$order_names output:
array(2) {
["name"]=>
string(17) "Hotel 1"
["id"]=>
string(4) "5896"
}
array(2) {
["name"]=>
string(26) "Hotel 2"
["id"]=>
string(4) "6005"
}
Now I need to add this data in the array given above. I'm not sure how to achieve this, can someone help me.
In the loop, after $order_names assignment, add :
$originalArray['options'][$order_names['id']] = $order_names['name'];
I assume your first array at the top is called $a.
So you can append an array element to your 'options' sub array like this:
foreach ($order_names as $order_name) {
array_push($a['options'], array($order_name['id'] => $order_name['name']));
}
So I have these two arrays that I need to merge into one and keep the indexes. I tried different things like array_merge, array_merge_recursive, etc but I couldn't figure it out.
array(3) {
[0]=>
string(4) "Peter"
[1]=>
string(4) "Josh"
[2]=>
string(4) "Jasper"
}
array(3) {
[0]=>
string(2) "18"
[1]=>
string(2) "19"
[2]=>
string(2) "25"
}
This is how I want it to look like:
array(3) {
[0]=>
array(2) {
["name"]=>
string(5) "Peter"
["age"]=>
int(18)
}
[1]=>
array(2) {
["name"]=>
string(4) "Josh"
["age"]=>
int(19)
}
}
Any ideas how I can achieve this?
Try This:
$array1 = array(0 => 'Peter', 1 => 'Josh', 2 => 'Jasper');
$array2 = array(0 => '18', 1 => '19', 2 => '25');
for($i = 0; $i<count($array1); $i++){
$newArray[] = array("name" => $array1[$i], "age" => $array2[$i]);
}
var_dump($newArray);
This is something I've used before as more reusable decision in case you want to add for example not only Name, Age, but Email or something else later.
Posting below code with explained comments.
<?php
// Array containing names
$namesArr = array(
'Peter',
'Josh',
'Jasper'
);
// Array containing ages
$agesArr = array(
18,
19,
25
);
$arrayDesign = array(
'name' => $namesArr,
'age' => $agesArr
);
/**
* Combines given array design into one grouped by keys.
* Example Input:
* $design => array(
* 'name' => array('Name1', 'Name2'),
* 'age' => array(10, 20)
* );
* Example Output:
* $output => array(
* 0 => array(
* 'name' => 'Name1',
* 'age' => 10
* ),
* 1 => array(
* 'name' => 'Name2',
* 'age' => 20
* )
* );
*
* #param Array $arrayDesign
*
* #return Array combined array
*/
function combineArraysByKeys($arrayDesign) {
// Holds results
$results = array();
// Get size of first element from the design, so we'll know the size of rest elements.
$designElementSize = count($arrayDesign[array_keys($arrayDesign)[0]]);
// Count from-to elements
for($c = 0; $c < $designElementSize; $c++) {
// Define array as part of results to be added after population
$arrayPart = array();
// Loop thru all keys and get values
foreach(array_keys($arrayDesign) as $key) {
// Assign value to key
$arrayPart[$key] = $arrayDesign[$key][$c];
}
// Add to results array
$results[] = $arrayPart;
}
return $results;
}
$result = combineArraysByKeys($arrayDesign);
echo "<PRE>";
print_r($result);
die();
I have two multidimensional arrays of the same structure.
Like this:
array(2) {
[0] =>
array(9) {
'id' =>
string(5) "44994"
'ersatzteil_id' =>
string(3) "120"
'lang' =>
string(6) "name2_tag2"
'title' =>
string(12) "Seitentüren"
'alias' =>
string(12) "seitentueren"
'content' =>
string(1610) "LOREM ISPUM BLALABLBL"
'on_main' =>
string(1) "0"
'disabled' =>
string(1) "1"
'short_text' =>
NULL
}
[1] =>
array(9) {
'id' =>
string(5) "44996"
'ersatzteil_id' =>
string(3) "122"
'lang' =>
string(6) "name1_tag1"
'title' =>
string(7) "Spoiler"
'alias' =>
string(7) "spoiler"
'content' =>
string(1513) "SOME OTHER RANDOM TEXT"
'on_main' =>
string(1) "0"
'disabled' =>
string(1) "0"
'short_text' =>
NULL
}
}
What I need to do is I need to compare first array with the second one.
I have to compare them by keys ersatzteil_id and content , and I find that they have same content I need to store element from first array in another new array, that wasn't existing before.
For example I need something like this, but more efficient:
if(array1[20]['ersatzteil_id'] == array2[145]['ersatzteil_id']
&& array1[20]['content'] == array2[145]['content']){
array3 = array1[20];
}
Try this code:-
$result = [];
foreach($array1 as $arr1){
foreach($array2 as $arr2){
if(($arr1['id'] == $arr2['id']) && ($arr1['ersatzteil_id'] == $arr2['ersatzteil_id'])){
$result[] = $arr1;
}
}
}
echo '<pre>'; print_r($result);