Question : foreach array

So I'm filling an array with items from the database, and I want to show these items in the autocomplete textbox I've got. The problem is, that the array gets filled with items correctly, but the foreach doesn't seem to work at all.

The code is included, it's the last foreach that's giving the problems.
I really hope you can help, I've been stuck on this for a very long time.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
<?php
}

$text = 'ni';//strtolower($_GET["term"]);
if (!$text) return;
$sql = "SELECT artistID, artistname FROM artists WHERE artistname LIKE '%".mysql_real_escape_string($text)."%' LIMIT 5";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) 
{
	$items[] = array($row['artistname'] => $row['artistID']);
}

function array_to_json( $array ){

    if( !is_array( $array ) ){
        return false;
    }

    $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
    if( $associative ){

        $construct = array();
        foreach( $array as $key => $value ){

            // We first copy each key/value pair into a staging array,
            // formatting each key and value properly as we go.

            // Format the key:
            if( is_numeric($key) ){
                $key = "key_$key";
            }
            $key = "\"".addslashes($key)."\"";

            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "\"".addslashes($value)."\"";
            }

            // Add to staging array:
            $construct[] = "$key: $value";
        }

        // Then we collapse the staging array into the JSON form:
        $result = "{ " . implode( ", ", $construct ) . " }";

    } else { // If the array is a vector (not associative):

        $construct = array();
        foreach( $array as $value ){

            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "'".addslashes($value)."'";
            }

            // Add to staging array:
            $construct[] = $value;
        }

        // Then we collapse the staging array into the JSON form:
        $result = "[ " . implode( ", ", $construct ) . " ]";
    }

    return $result;
}

$result = array();
echo "<pre>" . print_r($items, true) . "</pre>";
foreach ($items as $key=>$value) {
	if (strpos(strtolower($key), $text) !== false) {
		array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
	}
	if (count($result) > 11)
		break;
}
echo array_to_json($result);


mysql_close($link);
?>

Answer : foreach array

Hi, Serellyn. Let me ask some questions, please.
1.You say that last foreach doesn't work at all: what are you meaning, exactly? You get some error message? Or simply echo statement return an empty string?
2. Have you tried to print $items at the top of the page? If not, I suggest to do it this way:
  <?
  while ($row = mysql_fetch_assoc($result))
  {
        $items[] = array($row['artistname'] => $row['artistID']);
  }
  print_r($items);exit();
  ?>
3. Have you yet tested your function array_to_json()? In other words, are you sure the error isn't in this function?

Waiting for you...
Random Solutions  
 
programming4us programming4us