Its a simple problem but I think i am stuck..I need to get the array's elements to be displayed in a tree format in php.. the array elements will be fetched from the database..
Recursively iterate over the array, test for arrayness with is_array(), other wise print out. Modify for whether you want preorder, inorder, or postorder. Done.
Related
I've been banging my head against a wall with this one. What I have is a database table which holds references such as {name} etc which map to an array which has been built such as $customer['name']. What I need to do is take the value from the database and get the value from the array.
The value in the database is the same as what would be needed to reference the array (so $customer['name'] would be in the database field).
Can anyone tell me how I would get the array value from this?
Thanks in advance.
I managed to find an answer from this thread - get value from array using string php. What I ended up doing was separating the array name and key with a full stop so $customer['name'] is now stored as customer.name and exploded it to get the array value.
When I retrieve the results from a database query in PHP, I receive duplicated values, where ones is an integer, and one has the column name. array_unique(), in sorting the array, would in most cases end up with the numeric key before the string, meaning that would be the key kept. Right now, I use a function that removes from the array anything with a numeric key, but I don't really care for this approach. Does anyone have a better way to do this?
Change the command you use to retrieve the values from the database (eg. mysql_fetch_assoc instead of mysql_fetch_array). No matter which api you use now, there is an alternative that does exactly what you want.
update:
In PDO you would write:
$nonumindexes = $res->fetch(PDO::FETCH_ASSOC);
I suspect you're using mysqli_fetch_array() to retrieve the results?
If so, the 2nd parameter allows you to retrieves results as an associative array, numeric array or both. Or you can simply use mysqli_fetch_row or mysqli_fetch_assoc to get the results in the format you want.
http://www.php.net/manual/en/mysqli-result.fetch-array.php
Hi i am facing one problem in passing the array elements to another array...
$order=$this->ro_model->get_ro_details($order_id);
By using above function i will get query results into array name called $order.
Now i had form when it submits i will get some data from that form that i am saving in another name called $order_details.
$order_data=array(
'amount'=>$this->input->post('amount'
);
Now i wanna save both the arrays data in one table in database. For that what i thought was i can pass first array data to second array data and i will save all the data in one array. Then later i will send that array to a function which will insert the data into database.
$this->ro_model->add_amount($order_data);
My problem how to pass that first array data to second array...........
Note: All these are i am doing in codeigniter framework.
Use array_merge() function. Here you go: http://php.net/manual/en/function.array-merge.php
you can do it in 2 ways
array_push Push one or more elements onto the end of array http://in3.php.net/manual/en/function.array-push.php
array_merge Merge one or more arrays http://in3.php.net/manual/en/function.array-merge.php
I have an array like array ( array 0 ('item1'=>1,'item2'=>3))....etc like so.
And I want to access say the value of item2 but I don't want to use array[0]['item2']...Is there another way to access it? I just need 1 value so I don't think putting it in a foreach loop would be efficient..
Any ideas?
You have two choices: if you know the row and column you want to access, then you can access it directly. If you don't know the row and column, then you'd need to use a foreach loop to find the item you want.
You can only access it directly if you know where it is.
Assuming you meant array ( 0 => array ('item1'=>1,'item2'=>3)), you can use
array_values() to renumber the top level values. Then you know that the first value ('item1'=>1,'item2'=>3) will be indexed by the key 0.
I want to be able to easily and quickly delete an array of needles from a haystack array. In actual fact I have a comma separated list of numbers (though this is a string i know) that need to be deleted from a second comma separated list of number that I pull from a field in my sql database.
So I need -
$orig_needle_list = "3456,5678";
The haystack list is in a field in a mysql database and is "3456, 4567, 5678, 6789" - so I basically want to update this field to "4567,6789"
Q1. Should I retrieve the haystack list, convert both to arrays and iterate over them with a nested foreach cycle and array_splice any matched values?
Q2 Can I use in_array to be faster than nested foreach method?
Q3 is there a way to cut out the middle man and update the field by performing this in an sql query?
thanks
you don't need to iterate over things, there's a function called array_diff:
http://www.php.net/manual/en/function.array-diff.php
So create 2 arrays of the comma separated list and use array_diff, the resulting array is the difference of these two.
Storing comma separated lists in a database isn't a good idea because it breaks normalization.
I think you are looking for array_intersect() http://php.net/manual/en/function.array-intersect.php
try this:
implode(",",array_diff(explode(",",$haystack),explode(",",$orig_needle_list)));