Thanks for helping as always.
My code generates arrays in the following format
${'readyformerge' . $b} = $temparraytwo;
Which results in the array names of
$readyformerge1
$readyformerge2
$readyformerge3
etc...
Which works well and we know the value that $b holds is the amount of arrays we need to merge. However I can't quite see how to do this when we won't know prior to running the script how many arrays will be created.
Fundamentally I would like to use the following to grab the result but as you see I can only do this for the amount of results I THINk it will return NOT the actual number of results. Any help?
$outputmultidimensionalevent = array_merge_recursive(${'readyformerge' . $b},${'readyformerge' . $b});
print_r($outputmultidimensionalevent); echo '<br/>';
Your problem is the result of bad design.
Instead of:
${'readyformerge' . $b} = $temparraytwo;
You should do something like this:
$readyformerge[$b] = $temparraytwo;
And then:
$merged = array();
foreach ($readyformerge as $one) {
$merged = array_merge($merged, $one);
}
Thanks, That's pushed me in the right direction.
So currently it's now setup like this:
$z=1;
$readyformergemulti = array();
while ($z <= $i){
array_push($readyformergemulti,${'readyformerge' . $z});
$z++;
}
foreach ($readyformergemulti as $one) {
print_r($one);
echo '<br/>';
$merged = array_merge_recursive($merged, $one);
}
print_r($readyformergemulti); echo '<br/>';
print_r($merged); echo '<br/>';
But unfortunately $merged returns nothing. If you look at the following the first 4 lines are the $readyformerge arrays and the 5th line is the desired result:
Array ( [house] => 2797 )
Array ( [house] => 2829 )
Array ( [house] => 2736 )
Array ( [electronica] => 2763 [house] => 2763 )
Array ( [electronica] => Array (2763) [house] => Array (2763,2797,2892,2736 ) )
Sorry to be a pain, and I KNOW everyone needs to see more code, but with thousands of lines it gets hard to display!
If you can help again that would be great!
Related
I have a php variable that contain value of textarea as below.
Name:Jay
Email:jayviru#demo.com
Contact:9876541230
Now I want this lines to in array as below.
Array
(
[Name] =>Jay
[Email] =>jayviru#demo.com
[Contact] =>9876541230
)
I tried below,but won't worked:-
$test=explode("<br />", $text);
print_r($test);
you can try this code using php built in PHP_EOL but there is little problem about array index so i am fixed it
<?php
$text = 'Name:Jay
Email:jayviru#demo.com
Contact:9876541230';
$array_data = explode(PHP_EOL, $text);
$final_data = array();
foreach ($array_data as $data){
$format_data = explode(':',$data);
$final_data[trim($format_data[0])] = trim($format_data[1]);
}
echo "<pre>";
print_r($final_data);
and output is :
Array
(
[Name] => Jay
[Email] => jayviru#demo.com
[Contact] => 9876541230
)
Easiest way to do :-
$textarea_array = array_map('trim',explode("\n", $textarea_value)); // to remove extra spaces from each value of array
print_r($textarea_array);
$final_array = array();
foreach($textarea_array as $textarea_arr){
$exploded_array = explode(':',$textarea_arr);
$final_array[trim($exploded_array[0])] = trim($exploded_array[1]);
}
print_r($final_array);
Output:- https://eval.in/846556
This also works for me.
$convert_to_array = explode('<br/>', $my_string);
for($i=0; $i < count($convert_to_array ); $i++)
{
$key_value = explode(':', $convert_to_array [$i]);
$end_array[$key_value [0]] = $key_value [1];
}
print_r($end_array); ?>
I think you need create 3 input:text, and parse them, because if you write down this value in text area can make a mistake, when write key. Otherwise split a string into an array, and after create new array where key will be odd value and the values will be even values old array
I am on creating a website, I need to process CSV files that my clients will bring from outside.
I have a question about my tables when processing CSV.
I wish I could treat the array of this mantle:
$Data[1];
$Data[2];
$Data[3];
Today the array comes out of this mangle:
$Data[line1];
But line 1 can vary and that's its the problem, I do not know how to display it dimensionally.
import.php
$result = $this->csvreader->parse_file('assets/csv/test.csv');
$data['csvData'] = $result;
$this->load->view('user/import',$data);
result.php
foreach($csvData as $field)
{
echo $field['unknowLine'];
}
I do not know if I am clear, but if you have a solution for me thanks in advance
using array_values()
array_values — Return all the values of an array
example:
$array = array('line1' => 'line', 'line2' => 'line2');
$data = array_values($array);
print_r($data);
// Output
// Array ( [0] => line [1] => line2 )
echo $data[0]; // Output : line
You can make a loop like this, this is pretty straightforward;
foreach($array as $key => $value) { echo $array[$key]; }
Hope this helps!
Regards,
I've started learning about arrays and they've very confusing. I want to generate 4 numbers using this:
$numbers = range(1,4);
Then I shuffle with this:
shuffle($numbers);
Now I want to get each number as a variable, I've been told the best way is arrays. I have this code:
foreach($numbers as $number){
$test = array("first"=>"$number");
echo $test['first'];
}
What this does is echo all 4 numbers together, like "3142" or "3241" etc. Which is close to what I want, but I need all 4 numbers to have their own variable each. So I made this:
foreach($numbers as $number){
$test = array("first"=>"$number","second"=>"$number","third"=>"$number","fourth"=>"$number");
echo $test['first']," ",$test['second']," ",$test['third']," ",$test['fourth']," <br>";
}
This just echoes the 4 numbers 4 times. I need "first" to be the first number, "second" to be the second number of the 4 and the same for the third and fourth. I've been searching the web but don't know specifically what to search for to find the answer.
If someone answers could they please put as much detail into what certain functions do as possible, I want to learn not just get working code :)
You can use sizeof() it is return size of array size, and pass the key value manually. Like-
echo $numbers[0];
echo $numbers[1];
echo $numbers[2];
Here is a full code, try it
$numbers = range(1,4); //generate four numbers
shuffle($numbers); //shuffle them
$test = array(); //create array for the results
$words = array("1st", "2nd", "3rd", "4th","5th"); //create your array keys
$i=0;
foreach($numbers as $number){
$test[] = array($words[$i]=>$number); //add your array keys & values
$i++;
}
print_r($test); //show your results
If my understanding is correct you have an array and you want certain values from within it?
Then why not just use the array keys:
$array = array('peach','pear','apple','orange','banana');
echo $array[0]; // peach
echo $array[1]; // pear
echo $array[2]; // apple
Or you could loop through the array like so:
foreach ($array as $arrayKey => $arrayValue) {
// First value in the array is now below
echo $arrayKey; // 0
echo $arrayValue; // peach
}
You can also check if a value is in an array like so:
if (in_array('orange', $array)) {
echo 'yes';
}
Edit:
// Our array values
$array = array('peach','pear','apple','orange','banana');
// We won't shuffle for the example, we need expected results
#$array = shuffle($array);
// We want the first 3 values of the array
$keysRequired = array(0,1,2);
// This will hold our results
$storageArray = array();
// So for the first iteration of the loop $arrayKey is going to be 0
foreach ($array as $arrayKey => $arrayValue) {
// If the array key matches one of the values in the required array
if (in_array($arrayKey, $keysRequired)) {
// Store it within the storage array so we know what value it is
$storageArray[] = $arrayValue;
}
}
// Let's see what values have been stored
echo "<pre>";
print_r($storageArray);
echo "</pre>";
Would give you the following:
Array
(
[0] => 'peach'
[1] => 'pear'
[2] => 'apple'
)
Try this:
<?php
$numbers = range(1,4);
shuffle($numbers);
$test = array();
foreach($numbers as $k=>$v){
$test[$k] = $v;
}
echo "<pre>";
print_r($test);
?>
This will give you the output as:
Array
(
[0] => 3
[1] => 4
[2] => 2
[3] => 1
)
After that you can do:
$myvar["first"] = $test[0];
$myvar["second"] = $test[1];
$myvar["third"] = $test[2];
$myvar["fourth"] = $test[3];
Every array has a key, value pair, if you have an array say:
$myarr = array("a", "b", "c");
then you can access the value "a" as $myarr[0], "b" as $myarr[1] and so on.
In the for loop I am looping through the array with their key and value, this will not only give you the key of the array but also the value associated with that key.
More on Array
Edit:
Improving what Luthando Loot answered:
<?php
$numbers = range(1,4);//generate four numbers
shuffle($numbers);//shuffle them
$test = array();//create array for the results
$words = array("first", "second", "third", "fourth"); //create your array keys
$i = 0;
foreach($numbers as $number){
$test[$words[$i]] = $number;//add your array keys & values
$i++;
}
echo "<pre>";
print_r($test); //show your results
?>
Output:
Array
(
[first] => 4
[second] => 3
[third] => 2
[fourth] => 1
)
use this way
$numbers[0];
$numbers[1];
$numbers[2];
$numbers[3];
Firstly make an array of keys as
$key = ['first','second','third','fourth'];
And then you can simply use array_combine as
$numbers = range(1,4);
shuffle($numbers);
$key = ['first','second','third','fourth'];
$result = array_combine($key,$numbers);
Demo
I have 2 arrays - the first one is output first in full. The 2nd one may have some values that were already used/output with the first array. I want to "clean up" the 2nd array so that I can output its data without worrying about showing duplicates. Just to be sure I have the terminology right & don't have some sort of "array within an array", this is how I access each one:
1st Array
$firstResponse = $sth->fetchAll();
foreach ($firstResponse as $firstResponseItem) {
echo $firstResponseItem['samecolumnname']; // Don't care if it's in 2nd array
}
2nd Array
while( $secondResponseRow = $dbRequest->fetch_assoc() ){
$secondResponseArray = array($secondResponseRow);
foreach ($secondResponseArray as $secondResponseItem){
echo $secondResponseItem['samecolumnname']; //This can't match anything above
}
}
Thanks!
For example:
$response_names = array();
$firstResponse = $sth->fetchAll();
foreach ($firstResponse as $firstResponseItem)
$response_names[] = $firstResponseItem['samecolumnname'];
while( $secondResponseRow = $dbRequest->fetch_assoc() ){
$secondResponseArray = array($secondResponseRow);
foreach ($secondResponseArray as $secondResponseItem) {
if (!in_array($secondResponseItem['samecolumnname'], $response_names))
$response_names[] = $secondResponseItem['samecolumnname'];
}
}
array_walk($response_names, function($value) { echo $value . '<br />' });
If I understand what you're looking to do and the arrays are in the same scope, this should work.
$secondResponseArray = array($secondResponseRow);
$secondResponseArray = array_diff($secondResponseArray, $firstResponse);
//secondResponseArray now contains only unique items
foreach ($secondResponseArray as $secondResponseItem){
echo $secondResponseItem['samecolumnname'];
}
If you know that the keys of duplicate values will be the same you could use array_diff_assoc to get the items of the first array that aren't in the other supplied arrays.
This code
<?php
$a = array('abc', 'def', 'ghi', 'adg');
$b = array('abc', 'hfs', 'toast', 'adgi');
$r = array_diff_assoc($b, $a);
print_r($a);
print_r($r);
produces the following output
[kernel#~]php so_1.php
Array
(
[0] => abc
[1] => def
[2] => ghi
[3] => adg
)
Array
(
[1] => hfs
[2] => toast
[3] => adgi
)
[kernel#~]
i am so stuck , please someone help if you have 2 min.
i have a foreach where I get values from session something like
foreach ($result as $i => $item ) {
$values_array = explode("\n",$get_widths); // outputs Array ( [0] => 120 [1] => 180)
// here I need to echo the value from the array but only ONCE
}
the issue is that this is used by 2 or more items and array outputs gets repeated every time , so if 2 items i get
Array ( [0] => 120 [1] => 180) Array ( [0] => 120 [1] => 180) or
120 120
instead
120 180
for actual code see here
<?php
foreach ($list as $i => &$item){
$i=0;
$is_group = $item_params->get('is_group');
$group_widths = $item_params->get('group_widths');
$group_widths_array = explode("\n",$group_widths);
if($is_group == 1){
echo $group_widths_array[$i];
}
}
?>
please note I cant move outside the foreach. thank you
I would simply use a flag variable of some sort to determine if we have processed yet or not.
$values_array = explode("\n",$get_widths);
if (!isset($we_have_outputted)) {
echo '<pre>'. print_r($values_array, true) .'</pre>';
$we_have_outputted = true;
}
I am completely missing the point of this loop...
You are iterating over an array, assigning the key to $i and the value to $item but then you set $i to 0, echoing $group_widths_array[0] every time your condition is met and $item you don´t use at all...
That can´t be right, can it?
Anyway, it seems to me that the reason that your output is repeated is that you output $group_widths_array[0] every time and $is_group is the same on every iteration.