Tagged Questions

76
votes
5answers
43k views

Sort multidimensional Array by Value (2) [closed]

Possible Duplicate: How do I sort a multidimensional array in php How can I sort this array by the value of the "order" key? Even though the values are currently sequential, they will not ...
75
votes
8answers
14k views

PHP PDO: Can I bind an array to an IN() condition?

I'm curious to know if it's possible to bind an array of values to a placeholder using PDO. The use case here is attempting to pass an array of values for use with an IN() condition. I'm not very ...
51
votes
15answers
46k views

How to convert array to SimpleXML

How can I convert an array to a SimpleXML object in PHP?
116
votes
12answers
34k views

Preferred method to store PHP arrays (json_encode vs serialize)

I need to store a multi-dimensional associative array of data in a flat file for caching purposes. I might occasionally come across the need to convert it to JSON for use in my web app but the vast ...
15
votes
6answers
8k views

How to Flatten a Multidimensional Array?

Is it possible, in PHP, to flatten a (bi/multi)dimensional array without using recursion or references? I'm only interested in the values so the keys can be ignored, I'm thinking in the lines of ...
16
votes
4answers
14k views

Sorting an associative array in PHP

I have an array in this format: Array ( [0] => Array ( [text] => tests [language] => [advertiserCompetitionScale] => 5 ...
157
votes
6answers
144k views

How to delete an element from an array in php?

does anyone know an easy way to delete an element from a php array? I mean so that foreach($array) no longer includes that element. I thought that setting it to null would do it, but apparently not.
27
votes
11answers
61k views

How to search by key=>value in a multidimensional array in PHP

Is there any fast way to get all subarrays where a key value pair was found in a multidimensional array? I can't say how deep the array will be. Simple example array: $arr = array(0 => ...
41
votes
6answers
73k views

PHP/MYSQL using an array in WHERE clause

given an array $galleries = array ( [0] => 1 [1] => 2 [2] => 5 ) I want to have a sql query that uses the values of the array in its ...
17
votes
5answers
17k views

How to remove duplicate values from a multi-dimensional array in PHP

How can I remove duplicate values from a multi-dimensional array in PHP? Example array: Array ( [0] => Array ( [0] => abc [1] => def ) [1] => Array ( [0] => ...
10
votes
4answers
7k views

Transposing multidimensional arrays in PHP

How would you flip 90 degrees (transpose) a multidimensional array in PHP? For example: // Start with this array $foo = array( 'a' => array( 1 => 'a1', 2 => 'a2', 3 ...
12
votes
10answers
16k views

How to “flatten” a multi-dimensional array to simple one in PHP?

It's probably beginner question but I'm going through documentation for longer time already and I can't find any solution. I thought I could use implode for each dimension and then put those strings ...
27
votes
6answers
6k views

+ operator for array in PHP?

$test = array('hi'); $test += array('test','oh'); var_dump($test); What does + mean for array in PHP?
2
votes
3answers
732 views

Sorting a multidimensional array in PHP?

I have this kind of an array array(5) { [0]=> array(5) { [0]=> string(7) "jannala" [1]=> string(10) "2009-11-16" [2]=> string(29) " <p>Jotain ...
68
votes
5answers
6k views

List of Big-O for PHP functions

After using PHP for a while now, I've noticed that not all PHP built in functions as fast as expected. Consider the below two possible implementations of a function that finds if a number is prime ...
26
votes
7answers
8k views

php: access array value on the fly

in php, i often need to map a variable using an array ... but i can not seem to be able to do this in a one liner. c.f. example: // the following results in an error: echo array('a','b','c')[$key]; ...
5
votes
7answers
2k views

PHP syntax for dereferencing function result

In every other programming language I use on a regular basis, it is simple to operate on the return value of a function without declaring a new variable to hold the function result. In PHP, however, ...
47
votes
5answers
44k views

In PHP, how do you change the key of an array element?

I have an associative array in the form key => value where key is a numerical value, however it is not a sequential numerical value. The key is actually an ID number and the value is a count. This ...
27
votes
5answers
2k views

How is the PHP array implemented on the C level?

The PHP array is one of PHP's core features. It is sparse, allows multi-typed keys in the same array, and supports set, dictionary, array, stack/queue and iterative functionality. But after working ...
16
votes
6answers
17k views

Sort an array based on another array?

Is it possible in PHP to do something like this? How would you go about writing a function? Here is an example. The order is the most important thing. $customer['address'] = '123 fake st'; ...
66
votes
10answers
32k views

Java: function for arrays like PHP's join()?

I want to join a String[] with a glue string. Is there a function for this?
8
votes
1answer
320 views

Strange behavior Of foreach

<?php $a = array('a', 'b', 'c', 'd'); foreach ($a as &$v) { } foreach ($a as $v) { } print_r($a); ?> I think it's a normal program but this is the output I am getting: Array ( ...
10
votes
5answers
10k views

Best way to transfer an array between PHP and Javascript

So I have an array of records retreived from a database. The array is in the format; $rows[0]['id']=1; $rows[0]['title']='Abc'; $rows[0]['time_left']=200; $rows[1]['id']=2; $rows[1]['title']='XYZ'; ...
4
votes
6answers
2k views

Sorting multidimensional array in PHP

I am currently creating a sorting method that consists of values from an mysql query. Here's a brief view of the array: Array ( [0] => Array ( ['id'] = ...
2
votes
2answers
266 views

String with array structure to Array

I have string: Main.Sub.SubOfSub And some kind of data, may be a string: SuperData Also I have an empty array: $k = array(); How I can transform it all to this array above? Array ( [Main] ...
1
vote
2answers
295 views

Nested array. Third level is disappearing

I have that array : $a = array( "7" => array( "id" => 7, "parent" => 6 ), "6" => array( "id" => 6, "parent" => 5 ), "5" => ...
108
votes
30answers
24k views

PHP Arrays: A good way to check if an array is associative or numeric?

PHP treats all arrays as associative, so there aren't any built in functions. Can anyone recommend a fairly efficient way to check if an array contains only numeric keys? Basically, I want to be able ...
5
votes
5answers
2k views

I have an array of integers, how do I use each one in a mysql query (in php)?

I have an array full of random content item ids. I need to run a mysql query (id in the array goes in the WHERE clause), using each ID that's in the array, in the order that they appear in the said ...
11
votes
3answers
2k views

Casting an Array with Numeric Keys as an Object

I was poking around PHPs casting mechanism, and ran into an odd case when casting an array as an object $o = (object) array('1'=>'/foo/bar'); $o = new stdClass(); var_dump($o); As I understand ...
18
votes
2answers
13k views

What is the max key size for an array in PHP?

I am generating associative arrays and the key value is a string concat of 1..n columns. Is there a max length for keys that will come back to bite me? If so, I'll probably stop and do it ...
4
votes
3answers
5k views

php multi-dimensional array remove duplicate

Not sure if this question is a duplicate in need of removal, but I couldn't find the answer elsewhere so I'll have a go at asking. I've got a 2d array that looks as follows: Array ( [0] => Array ...
78
votes
3answers
2k views

Strange behaviour after loop by reference - Is this a PHP bug?

I just had some very strange behavior with a simple php script I was writing. I reduced it to the minimum necessary to recreate the bug: <?php $arr = array("foo", "bar", ...
25
votes
10answers
20k views

Sort Object in PHP

What is an elegant way to sort objects in PHP? I would love to accomplish something similar to this. $sortedObjectArary = sort($unsortedObjectArray, $Object->weight); Basically specify the array ...
7
votes
7answers
9k views

How to sort an array of UTF-8 strings?

I currentyl have no clue on how to sort an array which contains UTF-8 encoded strings in PHP. The array comes from a LDAP server so sorting via a database (would be no problem) is no solution. The ...
26
votes
7answers
16k views

Remove empty array elements

Some elements in my array are empty based on what the user has entered. I need to remove these elements. This is my code to do so: // Remove empty elements foreach($linksArray as $link) { ...
8
votes
8answers
3k views

PHP 2D Array output all combinations

I've had this problem bending my mind for a while now (head cold doesn't help either!), basically I have a PHP array which looks like this example: $array[0][0] = 'apples'; $array[0][1] = 'pears'; ...
1
vote
2answers
1k views

Convert flat array to the multi-dimentional

I have an array with tree data (by parent id). I want to convert it to multidimensional array. What is the best way to achieve that? Is there any short function for that? Source array: $source = ...
11
votes
5answers
2k views

Php array_push() vs myArray[]

If I understood properly you can add value to an array by using : $myArray[] = 123; or array_push($myArray, 123); Is one cleaner/faster then the other one ?
11
votes
11answers
21k views

php $_POST array empty upon form submission

I have a custom CMS i've built that works perfectly on my dev box (Ubuntu/PHP5+/MySQL5+). I just moved it up to the production box for my client and now all form submissions are showing up as empty ...
2
votes
2answers
624 views

PHP - Multiple uasort functions breaks sorting

I have a multidimensional array that stores people. Array ( id93294 => (array ( Name => "Tom Anderson", Birthday => "03/17/1975"), id29349 => (array ( ...
0
votes
5answers
1k views

highlight multiple keywords in search

i'm using this code to highlight search keywords: function highlightWords($string, $word) { $string = str_replace($word, "<span class='highlight'>".$word."</span>", $string); ...
19
votes
4answers
5k views

Access array returned by a function in php

I'm using a template engine that inserts code in my site where I want it. I wrote a function to test for something which is quite easy: myfunction() { return '($this->data["a"]["b"] ? true : ...
3
votes
4answers
1k views

Interleaving multiple arrays into a single array

I need to merge several arrays into a single array. The best way to describe what I'm looking for is "interleaving" the arrays into a single array. For example take item one from array #1 and ...
2
votes
2answers
2k views

Combinations, Dispositions and Permutations in PHP

What is the most efficient way to generate all the combinations, dispositions and permutations of an array in PHP?
18
votes
11answers
16k views

How do you reindex an array in PHP?

I have the following array, which I would like to reindex so the keys are reversed (ideally starting at 1): Current array (edit: the array actually looks like this): Array ( [2] => Object ( ...
14
votes
11answers
21k views

Save PHP array to MySQL?

What is a good way to save an array of data to a single mysql field? Also once I query for that array in the mysql table, what is a good way to get it back into array form? Is serialize and ...
12
votes
4answers
33k views

How to sort a date array in PHP

I have an array in this format: Array ( [0] => Array ( [28th February, 2009] => 'bla' ) [1] => Array ( [19th March, 2009] => 'bla' ...
16
votes
4answers
4k views

How are associative arrays implemented in PHP?

Can someone explain how PHP implements associative arrays? What underlying data structure does PHP use? Does PHP hash the key and store it in some kind of hash map? I am curious because I was ...
9
votes
4answers
18k views

passing arrays as url parameter

what is the best way that i can pass an array as a url parameter? i was thinking if this is possible: $aValues = array(); $url = 'http://www.example.com?aParam='.$aValues; or how about this: ...
7
votes
7answers
2k views

Directory to array with PHP

Whats the most efficient way of turning the filenames in a directory into an array of strings?

1 2 3 4 5 20
15 30 50 per page