Displaying two arrays in one foreach loop - php

I am making use of two arrays in one foreach loop.
Here's the code snippet that i have written for two input file type arrays.
$file[]= $_FILES['f_name']['name'];
$tmp_name[]=$_FILES['f_name']['tmp_name'];
foreach (array_combine($file, $tmp_name) as $code => $name) {
print_r($code);
print_r($name);
}
The resultant value i get on printing the array is this:
ArrayArray ( [0] => C:\xampp\tmp\phpC24D.tmp [1] => C:\xampp\tmp\phpC24E.tmp [2] => C:\xampp\tmp\phpC25F.tmp [3] => C:\xampp\tmp\phpC260.tmp [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
It prints only one array, doesn't prints the other array.
How can I get it printed?
thanks in advance.

Actually it is printing 2 arrays, just the 1st one contains nothing.
ArrayArray (
I prefer to use var_dump compared to print_r as it gives you more details for debugging.
As you are combining the 2 arrays into one and they only have 1 index there is nothing in the $code variable, just $name - unless you add array indexes.

Related

How do I change the array value?

Is it possible to change the highlighted word to numbers without changing it in database table?
wanted from this
$value['how']
to this
$value['0']
Yes, you need to use array_values()
$array = array("how" => "how-value", "to" => "to-value");
print_r(array_values($array));
Output:
Array
(
[0] => how-value
[1] => to-value
)
EDIT BY OP
To get the value
foreach($array as $key => $value) {
$someArray = array_values($value);
print_r($someArray[0]);
}
//return 144
#Milan Chheda answer is correct but I am just briefing here so the user can get a better idea of that.
Use array_values() to get the values of an array.
$FormedArray = array_values($your_array);
now print that array print_r($FormedArray);
You will get your result like
Array
(
[0] => 144
[1] => 41
[2] => USA
[3] => 12
[4] => 12
)
Here is a working example. https://eval.in/843720

Removing nth element from an array and reindex it

I have an array like below
Array
(
[0] => '13-Nov'
[1] => 'PUJA SUNUWAR'
[2] => '13-Nov'
[3] => '...301303'
[4] => 'TT1331600004\DLG'
[5] => '-10000.00'
[6] => '0'
[7] => '90000.00'
)
I need to remove 4th item of array and save it as
Array
(
[0] => '13-Nov'
[1] => 'PUJA SUNUWAR'
[2] => '13-Nov'
[3] => 'TT1331600004\DLG'
[4] => '-10000.00'
[5] => '0'
[6] => '90000.00'
)
i don't want to iterate over each elements of array. Is there any one shot function like array_pop to remove nth element of array?
use array_splice($array, 3, 1);
http://php.net/manual/en/function.array-splice.php
Is this a 2d-array? If so:
No, there is no built in function to do this. You could use ´array_walk´ with a custom callback but I doubt it would be faster than a simple foreach.
Else (if normal array):
unset( $aData[3] );
$aData = array_values( $aData );
Wich is faster then array_splice.
As mentioned above, there is no build in function for that.
You should use unset if you dont need to care about array indexing and array_values if should.
Also you should use value reference while array iterating to prevent internal array copyng while values modification. If dont, it can be one of perfomance break down reasons.
foreach ($yourDataSet as &$value) {
unset($value[2]);
//$value = array_values($value);
}
You shouldt use array_splice becouse of perfomance reasons.
p.s. Also you could check phpbench for perfomance test about different types of array iteration.

Add array to an array with the same indexes not being merged [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Combine Two Arrays with numerical keys without overwriting the old keys
OK guys, was searching about this one with no luck - it always points only to array_merge or array_push or array_combine functions which are useless for my purpose.
Here are two arrays (number indexed):
Array (
[0] => 12345
[1] => "asdvsdfsasdfsdf"
[2] => "sdgvsdfgsdfbsdf"
)
Array (
[0] => 25485
[1] => "tyjfhgdfsasdfsdf"
[2] => "mojsbnvgsdfbsdf"
)
and I need to create one "joined" (unioned) array, so it will look like:
Array (
[0] => 12345
[1] => "asdvsdfsasdfsdf"
[2] => "sdgvsdfgsdfbsdf"
[3] => 25485
[4] => "tyjfhgdfsasdfsdf"
[5] => "mojsbnvgsdfbsdf"
)
As I found nothing on this problem I tried by myself ($arr1 and $arr2 are the two small arrays):
$result_array = $arr1;
foreach($arr2 as $v) {
$result_array[] = $v;
}
This is, of course, working fine but I don't like this approach - imagine the situation when there will not be just 3 elements in second array...
Question: is there a better approach or at the best some built-in function (I do not know about)???
array_merge will work without any problem as your using numeric keys ... see the explanation below from the docs
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
emphasis mine
Array merge works fine for your numerically indexed arrays:
<?php
$arrayOne = array(
0 => 12345
,1 => "asdvsdfsasdfsdf"
,2 => "sdgvsdfgsdfbsdf"
);
$arrayTwo = array(
0 => 25485
,1 => "tyjfhgdfsasdfsdf"
,2 => "mojsbnvgsdfbsdf"
);
$arrayMerged = array_merge($arrayOne, $arrayTwo);
print_r($arrayMerged);
?>
output:
Array
(
[0] => 12345
[1] => asdvsdfsasdfsdf
[2] => sdgvsdfgsdfbsdf
[3] => 25485
[4] => tyjfhgdfsasdfsdf
[5] => mojsbnvgsdfbsdf
)

Printing Our Array Data

In PHP, Codeigniter: my array, $phoneList, contains the following:
Array
(
[name] => One, Are
[telephonenumber] => 555.222.1111
)
Array
(
[name] => Two, Are
[telephonenumber] => 555.222.2222
)
Array
(
[name] => Three, Are
[telephonenumber] => 555.222.3333
)
How do I list each name out? Each number out? Am I right in saying my array contains three different Arrays? And is that normal, for an array to contain arrays?
When I do a print_r($phoneList), I get the following:
Array ( [0] => Array ( [name] => One, Are [telephonenumber] => 555.222.1111 ) [1] => Array ( [name] => Two, Are [telephonenumber] => 555.222.2222 ) [2] => Array ( [name] => Three, Are [telephonenumber] => 555.222.3333 ) )
You'll probably want to use foreach to loop through them. Something like this:
foreach($data as $arr) { // assuming $data is the variable that has all this in
echo $arr['name'].": ".$arr['telephonenumber']."<br />";
}
Here is the solution. Foreach is the easiest approach.
It's completely normal to have an array of arrays (in this case an array of associative arrays). They can be written like so:
$arrayofarray = array(array('name' => 'aname', 'phone'=>'22233344444'), array('name' => 'bobble', 'phone'=>'5552223333'));
print_r($arrayofarray);
and you should be able to print out the content in this way:
foreach ($arrayofarray as $arr){
print $arr['name']."\n";
print $arr['phone']."\n";
}
If you want to know what terms are set in each associative array you can use array_keys() to return them (as a simple array). For example:
foreach ($arrayofarray as $arr){
$setterms=array_keys($arr);
foreach ($setterms as $aterm){
print "$aterm -> ".$arr[$aterm]."\n";
}
}

how do i print an element of an associative array when i dont know the key name

this array is generated and i want to be able to somehow grab the parent key without knowing the name, since i wont.
so the value i want is each array groups parent which would be zbench1, zbench2, .. and so on.
this array is already attached to a variable and i tried printing $myelements[0] but it gives an offset error.
Array
(
[zbench] => Array
(
[0] => editor-style.css
[1] => images
[2] => pagenavi-css.css
[3] => screenshot.png
[4] => style.css
)
[zbench1] => Array
(
[0] => editor-style.css
[1] => images
[2] => pagenavi-css.css
[3] => screenshot.png
[4] => style.css
)
[zbench2] => Array
(
[0] => editor-style.css
[1] => images
[2] => pagenavi-css.css
[3] => screenshot.png
[4] => style.css
)
[zbench3] => Array
(
[0] => editor-style.css
[1] => images
[2] => pagenavi-css.css
[3] => screenshot.png
[4] => style.css
)
)
You can get the values with array_keys().
foreach ($array as $key=>$value)
Or, if you have the key in a variable but don't a priori know what it is:
$array[$variable_holding_key]
Or if you just want to know what the keys are, but not necessarily do anything with them (all):
array_keys($array)
By definition, the inner arrays are only reachable via keys in the containing array - you have to know which parent key to use to access the appropriate child array. Is there a difference between each of those child arrays, other than having a slightly different parent key?
You can get all the keys in an array via array_keys(), which returns the keys as values in another array. Or you can use foreach($your_array as $key => $val) to iterate over each element in the array and get its associated key at the same time.
current() may work, it returns what the cursor is pointing to.
$element[key($element)]
array_shift, but this removes the last item.

Categories