Suppose I had a excel xlsx document called names with a couple of entries and wanted to print out the names from a php file.
include 'simplexlsx.class.php';
$xlsx = new SimpleXLSX('names.xlsx');
print_r( $xlsx->rows() );
This would print:
Array ( [0] => Array ( [0] => John [1] => Smith ) [1] => Array ( [0]
=> Steven [1] => Brake ) [2] => Array ( [0] => Carter [1] => Firen ) [3] => Array ( [0] => Juan [1] => Lahyno ) [4] => Array ( [0] => Sarah
[1] => Parker ) [5] => Array ( [0] => Julie [1] => Roberts ) [6] =>
Array ( [0] => Will [1] => Smith ) [7] => Array ( [0] => Angelina [1]
=> Jolie ) )
Is there a way to print out the data in a readable way? Or does print_r not allow formatting of an array passed to it?
If you add
echo '<pre>';
print_r( $xlsx->rows() );
Then you see somethiink like this:
Array (
[0] => Array
(
[0] => John [1] => Smith
)
[1] => Array
(
[0] => Steven [1] => Brake
)
...
I hope this will help you
Related
I have 2 PHP arrays that look like this..
$array1
--------
Array
(
[0] => Array
(
[0] => 64
[1] => Apple
)
[1] => Array
(
[0] => 22
[1] => Pear
)
[2] => Array
(
[0] => 3
[1] => Raisin
)
[3] => Array
(
[0] => 15
[1] => Grape
)
[4] => Array
(
[0] => 11
[1] => Banana
)
[5] => Array
(
[0] => 4
[1] => Orange
)
)
$array2
--------
Array
(
[0] => Array
(
[0] => 22
[1] => Pear
)
[1] => Array
(
[0] => 11
[1] => Banana
)
)
I want to merge the arrays together but put the matching items from $array2 at the top so the result would look like this...
$array3
-------
Array
(
[0] => Array
(
[0] => 22
[1] => Pear
)
[1] => Array
(
[0] => 11
[1] => Banana
)
[2] => Array
(
[0] => 64
[1] => Apple
)
[3] => Array
(
[0] => 3
[1] => Raisin
)
[4] => Array
(
[0] => 15
[1] => Grape
)
[5] => Array
(
[0] => 4
[1] => Orange
)
)
I'm not sure how to approach, should I merge the two first and then try and do some ordering, or is there a more efficient approach?
Get the 2nd array and then a rest of the 1st array
array_merge($arr2, array_udiff($arr1, $arr2, function($i1, $i2) {return $i1[0]-$i2[0];}));
UPDATED after trying the below suggestion it now gives the correct affect but enters it to many times.
Hi I have the following code what this does is creates two arrays then blend them into one:
$list_claimed_users = get_users('meta_key=claimed');
foreach ( $list_claimed_users as $list_claimed_user ) {
$a = get_user_meta($list_claimed_user->ID , "claimed");
$nicknames[] = get_user_meta($list_claimed_user->ID , "nickname");
$unserialized[] = unserialize($a[0]);
foreach($unserialized as $key => &$metadata) array_push($metadata, $nicknames[$key]);
}
$claimed_array[] = $unserialized;
print_r($claimed_array);
BUt it now loops and adds the $a data in an extra time for each loop
Array
(
[0] => Array
(
[0] => Array
(
[0] => 31.08.2016
[1] => prize8
[2] => Array
(
[0] => test5
)
[3] => Array
(
[0] => test5
)
[4] => Array
(
[0] => test5
)
[5] => Array
(
[0] => test5
)
)
[1] => Array
(
[0] => 31.07.2017
[1] => prize7
[2] => Array
(
[0] => test6
)
[3] => Array
(
[0] => test6
)
[4] => Array
(
[0] => test6
)
)
[2] => Array
(
[0] => 31.08.2017
[1] => prize5
[2] => Array
(
[0] => test7
)
[3] => Array
(
[0] => test7
)
)
[3] => Array
(
[0] => 21.08.2017
[1] => prize6
[2] => Array
(
[0] => test8
)
)
)
)
Could anyone please advise the best way to merge the two if if there is a simplier way to achieve this when creating the arrays originally.
Any help would be hugely appreciated
After retrieving the nicknames and the meta-data, but before adding result to $claimed_array, you can do:
foreach($a as $key => &$metadata) array_push($metadata, $nicknames[$key]);
Then just add $a to the $claimed_array:
$claimed_array[] = $a;
I'm trying to combine a single-dimensional array with a multi-dimensional array. I see that there is an array_combine function and an array_merge function, but they don't seem to give me the result I need. I have the following arrays:
$days = Array (
[0] => Array (
[0] => 3
[1] => 6
)
[1] => Array (
[0] => 6
[1] => 12
)
[2] => Array (
[0] => 2
[1] => 4
)
)
$names = Array (
[0] => Joe Smith
[1] => John Doe
[2] => Jack Frost
)
and this is the result I get when using array_merge($days,$names):
$result = Array (
[0] => Array (
[0] => 3
[1] => 6
)
[1] => Array (
[0] => 6
[1] => 12
)
[2] => Array (
[0] => 2
[1] => 4
)
[3] => Joe Smith
[4] => John Doe
[5] => Jack Frost
)
How do I get the following result:
$result = Array (
[0] => Array (
[0] => John Smith
[1] => Array (
[0] => 3
[1] => 6
)
)
[1] => Array (
[0] => John Doe
[1] => Array (
[0] => 6
[1] => 12
)
)
[2] => Array (
[0] => Jack Frost
[1] => Array (
[0] => 2
[1] => 4
)
)
)
Any ideas? Thanks
Using example #4 from the docs for array_map(), here's a cool way to do it:
$result = array_map(null, $names, $days);
I have a a multi-dimensional array, whose name is: $MyArray = array();.
This array can be used for showing the relation between things.
Array
(
[parent] => Array
(
[0] => Array
(
[1] => tom
[2] => bob
)
[1] => Array
(
[1] => liza
[2] => bob
)
[2] => Array
(
[1] => bob
[2] => john
)
[3] => Array
(
[1] => pet
[2] => john
)
)
[gender] => Array
(
[0] => Array
(
[1] => tom
[2] => male
)
[1] => Array
(
[1] => liza
[2] => female
)
[2] => Array
(
[1] => bob
[2] => male
)
[3] => Array
(
[1] => pet
[2] => female
)
)
)
Now, I want to return an array, which must be satisfied with all conditions:
Finds in the gender array, what line has the male value. This value is called: TempArray.
In what line of the TempArray; and, continue to finds what name in Sol_1 into the parent array.
Then returns into a multi-dimensional array: ResArray, like:
Array
(
[father] => Array
(
[0] => Array
(
[1] => tom
[2] => bob
)
[1] => Array
(
[1] => bob
[2] => john
)
)
[mother] => Array
(
[0] => Array
(
[1] => liza
[2] => bob
)
[1] => Array
(
[1] => pet
[2] => john
)
)
)
How to do it, with PHP?
i have big problem, because i don't know how get values from this array where value is be key into new array. This is my source array
Array
(
[0] => Array
(
[ID] => 250602
[NAME] => qwe
)
[1] => Array
(
[ID] => 250603
[NAME] => wer
)
[2] => Array
(
[ID] => 250629
[NAME] => sdf
)
[3] => Array
(
[ID] => 250629
[NAME] => xcv
)
[4] => Array
(
[ID] => 250629
[NAME] => fghfgh
)
[5] => Array
(
[ID] => 250601
[NAME] => pggd
)
[6] => Array
(
[ID] => 250601
[NAME] => dfgdfg
)
[7] => Array
(
[ID] => 250606
[NAME] => dfgdfg
)
)
When id is the same it will be created a new table that will look like for id = 250629
[NAME] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
How about foreach loop like this?
<?php
$final_array=array();
foreach($arrays as $sub_arr){ //it will traverse loop for all sub-arrays
$final_array[$sub_arr['ID']][]=$sub_arr['NAME'];
}
print_r($final_array); //you should see expected output.
?>
It will product below output for your given data:
Array
(
[250602] => Array
(
[0] => qwe
)
[250603] => Array
(
[0] => wer
)
[250629] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
[250601] => Array
(
[0] => pggd
[1] => dfgdfg
)
[250606] => Array
(
[0] => dfgdfg
)
)
Working Demo
Like this
$by_name = array();
foreach($your_array as $item)
$by_name[$item['ID']] []= $item['name'];
This makes use of php's lazy array initialization ([]= creates a new array implicitly).
If you get your array from mysql, you might also consider GROUP_CONCAT.